Reflection: how to find the object of information about the object, if this property has a non-public (private / protected) setter?

I was looking on the forum / Internet for a solution on how a PropetryInfo (public property) object can show if it has a private / secure setter ... everything was in vain .... all the help I found on how to "install "the value of public property having a private setter ...

I would like to know if I have a Public Property PropertyInfo object, how can I find out if its setter is open?

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

Help will be very matched ...

eg.

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

Final class

thanks

sankhe.

+3
2

PropertyInfo, GetSetMethod, MethodInfo . MethodInfo IsPublic, , .

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
+7

, , :

    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 .

0

Source: https://habr.com/ru/post/1716210/


All Articles