vendredi 8 mai 2015

How do I get the System.Type of a static member using reflection?

I have two static classes, one nested within the other like so:

public static class ClassA
{
    private static class ClassB
    {
        ...
    }
}

I would like to get the System.Type object of ClassB using reflection. Without reflection it would be as simple as this:

Type t = typeof(ClassB);

However, it is necessary to determine this type after the application has been compiled. Here is what I have so far:

// in this case I know that there is exactly one ClassB 
// so for simplicity's sake I have referenced the first element within the array
// the member info struct is filled correctly with information about ClassB.
System.Reflection.MemberInfo memberInfo = typeof(ClassA).GetMember("ClassB", 
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)[0];

// the type returned here does not reflect ClassB
Type t = memberInfo.GetType();





Aucun commentaire:

Enregistrer un commentaire