Home All Groups Group Topic Archive Search About

Reflect on a property of a property

Author
1 Nov 2007 6:25 PM
Barry Gilbert
I have a class that has a property of a type of another class. Like this:

Public Class Employee
   Public Property Shift As Shift
   ' Get and set stuff
   End Property
End Class

Public Class Shift
   Public Property ShiftId As Integer
   ' Get and set stuff
   End Property
End Class

Using reflection, I need to be able to get to the value contained in ShiftId
from looking at an Employee object. I've come close, but I cannot get the
right syntax. Here's what I have so far (this is in a base class):

Dim myType As Type=Me.GetType
For Each mi As MemberInfo In itemType.GetMembers(BindingFlags.Public Or _
                BindingFlags.SetProperty Or BindingFlags.Instance Or _
                BindingFlags.Static Or BindingFlags.NonPublic) myType.GetProperty(mi.Name).GetValue(me,nothing).GetType.GetProperty("ShiftId").GetValue(

I'm stuck on the object to pass into the last GetValue call. I;m not sure
how to get a reference to the object contained in the outer class's property.

Author
1 Nov 2007 10:30 PM
Mattias Sjögren
Barry,

>Dim myType As Type=Me.GetType
>For Each mi As MemberInfo In itemType.GetMembers(BindingFlags.Public Or _
>                BindingFlags.SetProperty Or BindingFlags.Instance Or _
>                BindingFlags.Static Or BindingFlags.NonPublic)
>myType.GetProperty(mi.Name).GetValue(me,nothing).GetType.GetProperty("ShiftId").GetValue(
>
>I'm stuck on the object to pass into the last GetValue call. I;m not sure
>how to get a reference to the object contained in the outer class's property.

You're already getting the reference (with GetValue), you just have to
hold on to it

Dim shift As Object
Dim shiftId As Object
shift = myType.GetProperty(mi.Name).GetValue(me,nothing)
shiftId = shift.GetType.GetProperty("ShiftId").GetValue(shift,
Nothing)


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
1 Nov 2007 11:37 PM
Barry Gilbert
Mattias,
Thanks for your reply. I came to the same conclusion.
For posterity, I ended up overriding ToString in my classes to be able to
not have to go down a level.

Thanks,
Barry

Show quote
"Mattias Sjögren" wrote:

> Barry,
>
> >Dim myType As Type=Me.GetType
> >For Each mi As MemberInfo In itemType.GetMembers(BindingFlags.Public Or _
> >                BindingFlags.SetProperty Or BindingFlags.Instance Or _
> >                BindingFlags.Static Or BindingFlags.NonPublic)
> >myType.GetProperty(mi.Name).GetValue(me,nothing).GetType.GetProperty("ShiftId").GetValue(
> >
> >I'm stuck on the object to pass into the last GetValue call. I;m not sure
> >how to get a reference to the object contained in the outer class's property.
>
> You're already getting the reference (with GetValue), you just have to
> hold on to it
>
> Dim shift As Object
> Dim shiftId As Object
> shift = myType.GetProperty(mi.Name).GetValue(me,nothing)
> shiftId = shift.GetType.GetProperty("ShiftId").GetValue(shift,
> Nothing)
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>

AddThis Social Bookmark Button