urgent Q about System.Reflection (PropertyInfo)

D

David

I have a class that whose properties include an object.

How can I read the sub object's property?

public class SubClass{
public int Number;
}

public class BigClass{
public SubClass subClass = new SubClass();
BigClass(){
subClass.Number = 10;
}
}

I tried..

Type t = Type.GetType("BigClass");
BigClass b = new BigClass();

PropertyInfo a = t.GetProperty("subClass.Number");

//at this point a is a null reference. I know I can do
PropertyInfo a = t.GetProperty("subClass");

and then use .GetValue to read the subclass's property, but without going
into detail, that will mean I'd have to

SubClass b = (SubClass)a.GetValue(b.subClass);

The problem is, at run time, I won't know that the type is SubClass. Is
there a way to find that out and execute a line like that above using
System.Reflection?

Sorry, it's late and my brain is scrambled.
 
S

Simon Smith

I have a class that whose properties include an object.

How can I read the sub object's property?

public class SubClass{
public int Number;
}

public class BigClass{
public SubClass subClass = new SubClass();
BigClass(){
subClass.Number = 10;
}
}

I tried..

<snip attempts using reflection>

BigClass b = new BigClass();
Int littleClassesNumber = b.SubClass.Number;

The problem is, at run time, I won't know that the type is SubClass. Is
there a way to find that out and execute a line like that above using
System.Reflection?

Sorry, it's late and my brain is scrambled.

Why won't you know the type at run time? The example given will always
be SubClass. Are you doing something rather more complex? (I won't start
on the evilitude of exposing members in the way you are doing, I'll assume
it's just for shortening the example code :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top