Properties of a Property

  • Thread starter Thread starter OpticTygre
  • Start date Start date
O

OpticTygre

Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?
 
Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?

With reflection, check the PropertyInfo.CanRead and CanWrite
properties.


Mattias
 
Ok, so I took your advice and looked into reflection, and here's a quick and
dirty of what I came up with:

Private Sub frmObject_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Text = CType(Me.Tag, CommandOperationBase).GetType.ToString

Dim a As Assembly = Assembly.GetAssembly(Me.Tag.GetType)
Dim s As String = String.Empty

For Each t As Type In a.GetExportedTypes
For Each mi As MemberInfo In t.GetMembers
If mi.DeclaringType Is Me.Tag.GetType Then
If mi.MemberType = MemberTypes.Property Then
s &= mi.Name & vbCrLf
End If
End If
Next
Next

MessageBox.Show(s)

End Sub

Basically, I created a new instance of frmObject from another form, set its
Tag property equal to an object, then load the form. This code just
displays all of the properties of that object.

But therein lies the problem. I don't just need the properties of that
object, but also of the object it inherits (all the way back to the base
object) as well. How can I change the code to do this?

Thanks again,

Jason
 
For Each t As Type In a.GetExportedTypes
For Each mi As MemberInfo In t.GetMembers
If mi.DeclaringType Is Me.Tag.GetType Then
If mi.MemberType = MemberTypes.Property Then
s &= mi.Name & vbCrLf
End If
End If
Next
Next

You're loading a lot of unneccesary metadata here for types and
members that you're not interested in. A simpler way would be

For Each mi As MemberInfo In Me.Tag.GetType().GetProperties()
s &= mi.Name & vbCrLf
Next

But therein lies the problem. I don't just need the properties of that
object, but also of the object it inherits (all the way back to the base
object) as well. How can I change the code to do this?

You should see them as well. GetProperties (and GetMethods) by default
return all the properties in the inheritance hierarchy, unless you
specify BindingFlags.DeclaredOnly.


Mattias
 
Back
Top