tags:

views:

28

answers:

2

I have a large class with many nested subclasses of different types as follows:

class BigFooClass
{
    // Classes
    Foo InnerFoo {get; set;}
    Bar InnerBar {get; set;}
    Oof InnerOof {get; set;}
    Rab InnerRab {get; set;}

    // Simple Properties
    Decimal OuterDecimal {get; set;}
    Long OuterLong {get; set;}
{

Each of these inner classes are defined as follows:

class Foo
{
   Decimal DecimalProp {get; set;}
   Long    LongProp {get; set;}
}
class Bar
{
   Decimal Decimal Prop {get; set;}
   Long    LongProp {get; set;}

} etc...

I want to get a list of ALL Decimal or Long properties together with their container types as follows:

BigFooClass.OuterDecimal is type of Decimal

BigFooClass.OuterLong is type of Long

Foo.OuterDecimal is type of Decimal

Foo.OuterLong is type of Long

Bar.OuterDecimal is type of Decimal

Bar.OuterLong is type of Long

I can get to the first level but cannot find how to reflect the type from a PropertyInfo, which may not be the correct way to do it.

Can anyone show me how to do it please?

Brian

A: 

Once you have the first level as a PropertyInfo, you'll need to recurse down another level and check the properties within that type. You can do this by looking at the PropertyInfo.PropertyType member, and then using that type to call GetProperty/GetProperties and fetch the "second level" properties.

Reed Copsey
Thanks Reed. In fact the missing piece of information for me was the use of PropertyType to get the type of the lower level class to do the iteration on the properties there. I had been trying to get the properties of the PropertyInfo!
Redeemed1
@Redeemed1: Glad that helped. When using reflection, you almost always need to work from a System.Type.
Reed Copsey
+1  A: 

Simple:

PropertyInfo pi = // get your property info here

pi.PropertyType;  // This is what you're looking for. (Type)
Aren
@Aren B, many thanks for this. @Reed Copsey gavce a bit more info in his response so I will give him the answer but I will up-vote this answer for you.
Redeemed1