您不能没有反思。但是,您 可以 通过反射来实现。这是一个完整的示例:
using System;using System.Reflection;public class Generic<T>{ public Generic() { Console.WriteLine("T={0}", typeof(T)); }}class Test{ static void Main() { string typeName = "System.String"; Type typeArgument = Type.GetType(typeName); Type genericClass = typeof(Generic<>); // MakeGenericType is badly named Type constructedClass = genericClass.MakeGenericType(typeArgument); object created = Activator.CreateInstance(constructedClass); }}
注意:如果泛型类接受多种类型,则在省略类型名称时必须包含逗号,例如:
Type genericClass = typeof(IReadOnlyDictionary<,>);Type constructedClass = genericClass.MakeGenericType(typeArgument1, typeArgument2);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)