ric*_*lla 5 c# types namespaces
我对C#错误感到有些困惑.
Type t = thing.GetType()
t现在是一种类型.但如果我试图这样做:
new GenericThing<t>
我得到一个警告说预期的类型或命名空间.我错过了什么?
t是在运行时创建的 Type 对象。泛型需要一个类型名称,在编译时解析。要在运行时创建泛型,您必须使用MakeGenericType
例如:
Activator.CreateInstance(typeof(GenericThing<>).MakeGenericType(t));
Run Code Online (Sandbox Code Playgroud)