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.
Best Answer
Once you have the
PropertyInfo
for the property, you can call itsGetSetMethod
function to return theMethodInfo
for the set accessor. You can then check theMethodInfo
'sIsPublic
property to see if the set accessor is public.