tags:

views:

1448

answers:

1

I've got a class that looks something like this:

public class Parent
{
    public class Subclass
    {
    }
}

and using reflection I'm trying to find the subclass

void main
{
    Parent p = new Parent();
    Type t = p.GetType();
    Type s = t.GetNestedType("Subclass"); //s is not set
}

This doesn't work because there apparently are no nested types. How can I find the type of the subclass? The reason I need to get s is to later call .GetMethod("someMethod").Invoke(...) on it.

+1  A: 

I just tried the exact same thing, and it worked for me:

    public class ParentClass
    {
        public class NestedClass
        {

        }
    }

       private void button1_Click(object sender, EventArgs e)
        {
            Type t = typeof(ParentClass);
            Type t2 = t.GetNestedType("NestedClass");
            MessageBox.Show(t2.ToString());
        }

Are you sure the NestedClass is public?

BFree
Ah, I was reflecting on the wrong parent class. Thanks for verifying the code!
Frode Lillerud