【发布时间】:2011-01-25 22:47:44
【问题描述】:
我有一个字典对象
Dictionary<string, Type> dict = new Dictionary<string, Type>();
我还有一个 BaseClass 和一些子类,例如:ChildClass1、ChildClass2 等。 字典有值:
dict.Add("type1", typeof(ChildClass1));
dict.Add("type2", typeof(ChildClass2));
我的问题是 - 有没有办法做这样的事情:
BaseClass c = new <<get a type from the dict: dict[type1]>>()?
我只是想让我的解决方案更灵活,但我不确定这是否可能。 谢谢!
【问题讨论】:
-
正如 Sparkie 所回答的,Activator 可以为您做到这一点。如果您只需要每个子类的一个实例,您可能需要查看对象工厂或结构映射
-
这是宝贵的建议,谢谢!我还不熟悉这些概念。
标签: c# object silverlight-3.0 memory-management