标题有点模糊.我想知道的是,如果可能的话:
string typeName = <read type name from somwhere>;
Type myType = Type.GetType(typeName);
MyGenericClass<myType> myGenericClass = new MyGenericClass<myType>();
Run Code Online (Sandbox Code Playgroud)
显然,MyGenericClass被描述为:
public class MyGenericClass<T>
Run Code Online (Sandbox Code Playgroud)
现在,编译器抱怨'找不到类型或命名空间'myType'."必须有办法做到这一点.
是否可以实现以下代码?我知道它不起作用,但我想知道是否有解决方法?
Type k = typeof(double);
List<k> lst = new List<k>();
Run Code Online (Sandbox Code Playgroud) 是否可以在设计时不知道类型的情况下声明泛型的实例?
例:
Int i = 1;
List<typeof(i)> list = new List<typeof(i)>();
Run Code Online (Sandbox Code Playgroud)
我的类型可以是任何东西,而不是必须做:
List<int> list = new List<int();
Run Code Online (Sandbox Code Playgroud)