实际上,那么你们两个都采取了 set 和 get 方法,这里是一个例子:
Public Class ExPropertyInfo
Inherits PropertyInfo
Dim p As PropertyInfo
Public Sub New(ByVal [property] As PropertyInfo)
p = [property]
Dim accssors() As MethodInfo = [property].GetAccessors(True)
Select Case accssors.Length
Case 1
isassembly_ = accssors(0).IsAssembly
isfamily_ = accssors(0).IsFamily
isprivate_ = accssors(0).IsPrivate
ispublic_ = accssors(0).IsPublic
isstatic_ = accssors(0).IsStatic
isfamilyandassembly_ = accssors(0).IsFamilyAndAssembly
isfamilyorassembly_ = accssors(0).IsFamilyOrAssembly
Case 2
Dim method As MethodInfo = Nothing
If accssors(0).IsPrivate Then
If accssors(1).IsFamily Or accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsFamily Then
If accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsFamilyAndAssembly Or accssors(0).IsFamilyOrAssembly Then
If accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsAssembly Or accssors(0).IsPublic Then
If accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
End If
isassembly_ = method.IsAssembly
isfamily_ = method.IsFamily
isprivate_ = method.IsPrivate
ispublic_ = method.IsPublic
isstatic_ = method.IsStatic
isfamilyandassembly_ = method.IsFamilyAndAssembly
isfamilyorassembly_ = method.IsFamilyOrAssembly
End Select
End Sub
Public Overrides ReadOnly Property Attributes As Reflection.PropertyAttributes
Get
Return p.Attributes
End Get
End Property
Public Overrides ReadOnly Property CanRead As Boolean
Get
Return p.CanRead
End Get
End Property
Public Overrides ReadOnly Property CanWrite As Boolean
Get
Return p.CanWrite
End Get
End Property
Public Overrides ReadOnly Property DeclaringType As Type
Get
Return p.DeclaringType
End Get
End Property
Public Overloads Overrides Function GetAccessors(nonPublic As Boolean) As MethodInfo()
Return p.GetAccessors(nonPublic)
End Function
Public Overloads Overrides Function GetCustomAttributes(inherit As Boolean) As Object()
Return p.GetCustomAttributes(inherit)
End Function
Public Overloads Overrides Function GetCustomAttributes(attributeType As Type, inherit As Boolean) As Object()
Return p.GetCustomAttributes(attributeType, inherit)
End Function
Public Overloads Overrides Function GetGetMethod(nonPublic As Boolean) As MethodInfo
Return p.GetGetMethod(nonPublic)
End Function
Public Overrides Function GetIndexParameters() As ParameterInfo()
Return p.GetIndexParameters
End Function
Public Overloads Overrides Function GetSetMethod(nonPublic As Boolean) As MethodInfo
Return p.GetSetMethod(nonPublic)
End Function
Public Overloads Overrides Function GetValue(obj As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo) As Object
Return p.GetValue(obj, invokeAttr, binder, index, culture)
End Function
Public Overrides Function IsDefined(attributeType As Type, inherit As Boolean) As Boolean
Return p.IsDefined(attributeType, inherit)
End Function
Public Overrides ReadOnly Property Name As String
Get
Return p.Name
End Get
End Property
Public Overrides ReadOnly Property PropertyType As Type
Get
Return p.PropertyType
End Get
End Property
Public Overrides ReadOnly Property ReflectedType As Type
Get
Return p.ReflectedType
End Get
End Property
Public Overloads Overrides Sub SetValue(obj As Object, value As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo)
p.SetValue(obj, value, invokeAttr, binder, index, culture)
End Sub
Private ispublic_ As Boolean
Public ReadOnly Property IsPublic As Boolean
Get
Return ispublic_
End Get
End Property
Private isprivate_ As Boolean
Public ReadOnly Property IsPrivate As Boolean
Get
Return isprivate_
End Get
End Property
Private isassembly_ As Boolean
Public ReadOnly Property IsAssembly As Boolean
Get
Return isassembly_
End Get
End Property
Private isfamily_ As Boolean
Public ReadOnly Property IsFamily As Boolean
Get
Return isfamily_
End Get
End Property
Private isstatic_ As Boolean
Public ReadOnly Property IsStatic As Boolean
Get
Return isstatic_
End Get
End Property
Private isfamilyandassembly_ As Boolean
Public ReadOnly Property IsFamilyAndAssembly As Boolean
Get
Return isfamilyandassembly_
End Get
End Property
Private isfamilyorassembly_ As Boolean
Public ReadOnly Property IsFamilyOrAssembly As Boolean
Get
Return isfamilyorassembly_
End Get
End Property
结束类
Dim [property] As ExPropertyInfo = New ExPropertyInfo(GetType(Form1).GetProperty("abc", BindingFlags.NonPublic Or BindingFlags.Static))
PropertyInfo.GetAccessors 方法返回一个get 和set 方法的数组。