从不同 dll 中的字符串值表示中获取泛型类型
Get generic type from string value representation in different dlls
我有 3 种类型,全部来自不同的 dll,你找不到如何在字符串中存储通用类型以及如何创建实例:
Type type1 = GetType1();
Type type2 = GetType2();
string strClassGenericType = "Namespace.ClassName<,>, DllName";
Type template = // How to get the generic template type?
Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);
我不确定 this 是否符合我的要求。
我认为您正在尝试从字符串值创建类型。
尝试:
Type type = Type.GetType("System.String");
例如,从字符串创建文本框类型:
Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
见MSDN。
泛型的正确字符串表示是这样的:
"System.Collections.Generic.Dictionary`2[[System.String],[System.Object]]"
其中'2
表示泛型类型参数的个数。
有关完全限定的类型名称,请参阅 the fiddle。
如果您正在寻找没有指定泛型类型参数的泛型类型(所谓的开放泛型),那么它是这样的:
"System.Collections.Generic.Dictionary`2, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
在特定情况下,如果您希望对象的类型不知道其类型参数,则可以使用带有类型参数数量的反引号。这是 Tuple
:
的示例
Console.WriteLine(typeof (Tuple<,>).FullName); //For information only, outputs "System.Tuple`2"
var typeName = "System.Tuple`2";
var type = Type.GetType(typeName);
var generic = type.MakeGenericType(typeof (string), typeof (int));
Console.WriteLine(generic.FullName); //Outputs the type with the type parameters.
vcsjones 和 abatishchev 都有正确答案,但你错过了 dll。
感谢 vcsjones 我使用了这个:
string strGenericType = "Namespace.ClassName`2, DllName";
Type template = Type.GetType(strGenericType);
Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);
感谢 abatishchev 这更短了:
string strGenericType = "Namespace.ClassName`2[[Namespace1.Type1, Type1DllName],[Namespace2.Type2, Type2DllName]], DllName";
Type genericType = Type.GetType(strGenericType);
object instance = Activator.CreateInstance(genericType);
我有 3 种类型,全部来自不同的 dll,你找不到如何在字符串中存储通用类型以及如何创建实例:
Type type1 = GetType1();
Type type2 = GetType2();
string strClassGenericType = "Namespace.ClassName<,>, DllName";
Type template = // How to get the generic template type?
Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);
我不确定 this 是否符合我的要求。
我认为您正在尝试从字符串值创建类型。
尝试:
Type type = Type.GetType("System.String");
例如,从字符串创建文本框类型:
Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
见MSDN。
泛型的正确字符串表示是这样的:
"System.Collections.Generic.Dictionary`2[[System.String],[System.Object]]"
其中'2
表示泛型类型参数的个数。
有关完全限定的类型名称,请参阅 the fiddle。
如果您正在寻找没有指定泛型类型参数的泛型类型(所谓的开放泛型),那么它是这样的:
"System.Collections.Generic.Dictionary`2, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
在特定情况下,如果您希望对象的类型不知道其类型参数,则可以使用带有类型参数数量的反引号。这是 Tuple
:
Console.WriteLine(typeof (Tuple<,>).FullName); //For information only, outputs "System.Tuple`2"
var typeName = "System.Tuple`2";
var type = Type.GetType(typeName);
var generic = type.MakeGenericType(typeof (string), typeof (int));
Console.WriteLine(generic.FullName); //Outputs the type with the type parameters.
vcsjones 和 abatishchev 都有正确答案,但你错过了 dll。
感谢 vcsjones 我使用了这个:
string strGenericType = "Namespace.ClassName`2, DllName";
Type template = Type.GetType(strGenericType);
Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);
感谢 abatishchev 这更短了:
string strGenericType = "Namespace.ClassName`2[[Namespace1.Type1, Type1DllName],[Namespace2.Type2, Type2DllName]], DllName";
Type genericType = Type.GetType(strGenericType);
object instance = Activator.CreateInstance(genericType);