I have the following scenario:
class A
{
   string Foo;
}
Class B
{
   A PropertyA;
}
Class C
{
   B PropertyB;
}
Is it possible using .NET reflection to get the value of A.Foo if I start with object C? The problem I am running into is this: I 开发者_如何学Goget to A through PropertyInfo objects. However, they don't have the instance information stored with them. Therefore, I can't do GetProperty("Foo").GetValue(....) since I only have object of type C passed in.
You have to get the object returned by each property, then use the same reflection procedure on that instance to get the next "level" deep.
For example:
 C instance = GetMyCInstance();
 B propertyB = instance.GetType().GetProperty("PropertyB").GetValue(instance) as B;
 A propertyA = propertyB.GetType().GetProperty("PropertyA").GetValue(propertyB) as A;
 string Foo = propertyA.GetType().GetProperty("Foo").GetValue(propertyA) as string;
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论