[英]How to I get reference to generic singleton instance using only System.Type/reflection
[英]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
是类。 只需将其转换为Type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.