反思:如何从属性信息对象中查找该属性是否具有非公共(私有/受保护)Setter?

发布于 2024-08-03 07:03:17 字数 1196 浏览 3 评论 0原文

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

我想知道我是否有公共属性的 PropertyInfo 对象,我如何知道它的 Setter 是否是非公共的?

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

非常感谢您的帮助...

例如

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

End Class

Thx

Vinit sankhe。

I searched on the forum / Internet for the solution how a PropetryInfo object (of a Public property) can reveal if it has a Private \ Protected Setter ... it was all in vain .... all help I found was about how to "Set" value of a public property having a Private Setter...

I would like to know if I have a PropertyInfo object of a public property, how would I know if its Setter is Non Public?

I tried, in a exception handling block, where I did a GetValue of the PropertyInfo object and then called SetValue by setting the same value back... but to my surprise it worked well and didn error out.

Help would be very much approaciated...

E.g.

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

End Class

Thx

Vinit sankhe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2024-08-10 07:03:17

获得 PropertyInfo 对于该属性,您可以调用其 GetSetMethod 函数返回 MethodInfo< /a> 用于设置访问器。然后,您可以检查 MethodInfoIsPublic 属性来查看设置的访问器是否是公共的。

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

Once you have the PropertyInfo for the property, you can call its GetSetMethod function to return the MethodInfo for the set accessor. You can then check the MethodInfo's IsPublic property to see if the set accessor is public.

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
小忆控 2024-08-10 07:03:17

其实,那么你就分别取一个位置给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

End Class

Dim [property] As ExPropertyInfo = New ExPropertyInfo(GetType(Form1).GetProperty("abc", BindingFlags.NonPublic Or BindingFlags.Static))

PropertyInfo.GetAccessors方法返回一个get和set方法的数组。

In fact, then you both take a position to set and get method, here is an example:

    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

End Class

Dim [property] As ExPropertyInfo = New ExPropertyInfo(GetType(Form1).GetProperty("abc", BindingFlags.NonPublic Or BindingFlags.Static))

PropertyInfo.GetAccessors the method returns a array of get and set the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文