Home All Groups Group Topic Archive Search About

Properties of a Property

Author
16 Feb 2006 7:47 PM
OpticTygre
Is it possible to detect if a property of a class is Readonly, WriteOnly, or
Read/Write?

Author
16 Feb 2006 9:06 PM
Mattias Sjögren
>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.
Author
17 Feb 2006 12:53 AM
OpticTygre
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

Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:ueR6QyzMGHA.1832@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.
Author
18 Feb 2006 10:58 PM
Mattias Sjögren
>    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.