3

我在论坛/互联网上搜索了解决方案(公共属性的)PropetryInfo 对象如何显示它是否具有 Private \ Protected Setter ......这一切都是徒劳的......我找到的所有帮助都是关于如何具有 Private Setter 的公共属性的“设置”值...

我想知道我是否有一个公共属性的 PropertyInfo 对象,我怎么知道它的 Setter 是否是非公共的?

我在异常处理块中尝试了对 PropertyInfo 对象执行 GetValue,然后通过设置相同的值来调用 SetValue……但令我惊讶的是,它运行良好并且没有出错。

帮助将非常approaciated...

例如

Public Class Class1
Public Property HasContextChanged() As Boolean
    Get
        Return _hasContextChanged
    End Get
    Protected Set(ByVal value As Boolean)
        _hasContextChanged = value
    End Set
End Property

Public Function CanWriteProperty(Optional ByVal propName As String = "HasContextChanged") As Boolean
    Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName)
    If prInfo Is Nothing Then Return False
    If Not prInfo.CanWrite Then
        Return False
    Else
        Try
            Dim value As Object = prInfo.GetValue(ownObj, Nothing)
            prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter
        Catch ex As Exception
            Return False 'Not coming here whatsoever
        End Try
    End If
    Return True
End Function

结束类

谢谢

维尼特桑克。

4

2 回答 2

7

一旦你有了PropertyInfofor 属性,你就可以调用它的GetSetMethod函数来返回MethodInfofor 设置访问器。然后,您可以检查MethodInfo'IsPublic属性以查看 set 访问器是否是公共的。

Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName)
Dim method as MethodInfo = prInfo.GetSetMethod()
If Not method.IsPublic Then
    Return False
Else
    Dim value As Object = prInfo.GetValue(ownObj, Nothing)
    prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter
End If
于 2009-08-27T13:52:56.727 回答
0

实际上,那么你们两个都采取了 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 方法的数组。

于 2013-12-30T16:54:08.983 回答