【发布时间】:2014-02-16 05:21:35
【问题描述】:
下面的代码不起作用,我想知道如何将实例动态转换为在运行时确定的类型?
Convert.ChangeType() 返回一个仍然需要转换的对象。所有尝试 Invoke() GetConstructor() 或 Activator.CreateInstance() 的尝试也是如此,见下文。在某些时候我需要显式地输入代码,我希望避免它或尽可能地推出它。
Type type = Type.GetType ("RandomChildClass");
Object obj = Activator.CreateInstance (type, new Object[]{ "argument" });
var instance = (type)obj;
我知道我可以创建一个方法来接受
【问题讨论】:
-
投完后你想用
instance做什么? -
只是将它添加到基本类型的列表中