By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,576 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,576 IT Pros & Developers. It's quick & easy.

Properties of a Property

P: n/a
Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?
Feb 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 16 '06 #2

P: n/a
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

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ue**************@TK2MSFTNGP11.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 17 '06 #3

P: n/a
> 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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.