[英]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.