如何使用反射获取静态成员的System.Type?

时间:2015-05-08 21:16:23

标签: c# reflection

我有两个静态类,一个嵌套在另一个中,如下所示:

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

我想使用反射获取ClassB的System.Type对象。如果没有反思,就会这么简单:

Type t = typeof(ClassB);

但是,在编译应用程序之后,有必要确定此类型。以下是我到目前为止的情况:

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

1 个答案:

答案 0 :(得分:3)

您的MemberInfo 该课程。只需将其投放到Type