简体   繁体   中英

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();

Your MemberInfo is the class. Just cast it to Type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM