如何使用反射获取静态成员的 System.Type?
How do I get the System.Type of a static member using 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();
你的 MemberInfo
是 class。只需将其转换为 Type
.
我有两个静态 类,一个嵌套在另一个中,如下所示:
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();
你的 MemberInfo
是 class。只需将其转换为 Type
.