本文整理匯總了VB.NET中System.Reflection.PropertyInfo.GetAccessors方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET PropertyInfo.GetAccessors方法的具體用法?VB.NET PropertyInfo.GetAccessors怎麽用?VB.NET PropertyInfo.GetAccessors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetAccessors方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: ClassWithProperty
' 導入命名空間
Imports System.Reflection
' Define a property.
Public Class ClassWithProperty
Private _caption As String = "A Default caption"
Public Property Caption As String
Get
Return _caption
End Get
Set
If _caption <> value Then _caption = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim test As New ClassWithProperty()
Console.WriteLine("The Caption property: {0}", test.Caption)
Console.WriteLine("----------")
' Get the type and PropertyInfo.
Dim t As Type = Type.GetType("ClassWithProperty")
Dim propInfo As PropertyInfo = t.GetProperty("Caption")
' Get all the accessors.
Dim methInfos() As MethodInfo = propInfo.GetAccessors()
Console.WriteLine("There are {0} accessors.",
methInfos.Length)
For ctr As Integer = 0 To methInfos.Length - 1
Dim m As MethodInfo = methInfos(ctr)
Console.WriteLine("Accessor #{0}:", ctr + 1)
Console.WriteLine(" Name: {0}", m.Name)
Console.WriteLine(" Visibility: {0}", GetVisibility(m))
Console.Write(" Property Type: ")
' Determine if this is the property getter or setter.
'' If (m.ReturnType == typeof(void))
If m.ReturnType Is GetType(Void) Then
Console.WriteLine("Setter")
Console.WriteLine(" Setting the property value.")
' Set the value of the property.
m.Invoke(test, { "The Modified Caption" } )
Else
Console.WriteLine("Getter")
' Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, {} ))
End If
Next
Console.WriteLine("----------")
Console.WriteLine("The Caption property: {0}", test.Caption)
End Sub
Private Function GetVisibility(m As MethodInfo) As String
Dim visibility As String = ""
If m.IsPublic Then
Return "Public"
ElseIf m.IsPrivate Then
Return "Private"
Else
If m.IsFamily Then
visibility = "Protected "
ElseIf m.IsAssembly Then
visibility += "Assembly"
End If
End If
Return visibility
End Function
End Module
輸出:
The Caption property: A Default caption ---------- There are 2 accessors. Accessor #1: Name: get_Caption Visibility: Public Property Type: Getter Property Value: A Default caption Accessor #2: Name: set_Caption Visibility: Public Property Type: Setter Setting the property value. ---------- The Caption property: The Modified Caption
示例2: ClassWithProperty
' 導入命名空間
Imports System.Reflection
' Define a property.
Public Class ClassWithProperty
Private _caption As String = "A Default caption"
Public Property Caption As String
Get
Return _caption
End Get
Set
If _caption <> value Then _caption = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim test As New ClassWithProperty()
Console.WriteLine("The Caption property: {0}", test.Caption)
Console.WriteLine("----------")
' Get the type and PropertyInfo.
Dim t As Type = Type.GetType("ClassWithProperty")
Dim propInfo As PropertyInfo = t.GetProperty("Caption")
' Get all the accessors.
Dim methInfos() As MethodInfo = propInfo.GetAccessors(True)
Console.WriteLine("There are {0} accessors.",
methInfos.Length)
For ctr As Integer = 0 To methInfos.Length - 1
Dim m As MethodInfo = methInfos(ctr)
Console.WriteLine("Accessor #{0}:", ctr + 1)
Console.WriteLine(" Name: {0}", m.Name)
Console.WriteLine(" Visibility: {0}", GetVisibility(m))
Console.Write(" Property Type: ")
' Determine if this is the property getter or setter.
'' If (m.ReturnType == typeof(void))
If m.ReturnType Is GetType(Void) Then
Console.WriteLine("Setter")
Console.WriteLine(" Setting the property value.")
' Set the value of the property.
m.Invoke(test, { "The Modified Caption" } )
Else
Console.WriteLine("Getter")
' Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, {} ))
End If
Next
Console.WriteLine("----------")
Console.WriteLine("The Caption property: {0}", test.Caption)
End Sub
Private Function GetVisibility(m As MethodInfo) As String
Dim visibility As String = ""
If m.IsPublic Then
Return "Public"
ElseIf m.IsPrivate Then
Return "Private"
Else
If m.IsFamily Then
visibility = "Protected "
ElseIf m.IsAssembly Then
visibility += "Assembly"
End If
End If
Return visibility
End Function
End Module
輸出:
The Caption property: A Default caption ---------- There are 2 accessors. Accessor #1: Name: get_Caption Visibility: Public Property Type: Getter Property Value: A Default caption Accessor #2: Name: set_Caption Visibility: Public Property Type: Setter Setting the property value. ---------- The Caption property: The Modified Caption