将Type作为参数传递给函数

Mas*_*sha 2 c# memory-management object silverlight-3.0

我有一个字典对象

Dictionary<string, Type> dict = new Dictionary<string, Type>();
Run Code Online (Sandbox Code Playgroud)

我还有一个BaseClass和一些子类,比如:ChildClass1,ChildClass2等.字典有值:

dict.Add("type1", typeof(ChildClass1));
dict.Add("type2", typeof(ChildClass2));
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 有没有办法做这样的事情:

BaseClass c = new <<get a type from the dict: dict[type1]>>()?
Run Code Online (Sandbox Code Playgroud)

我只是想让我的解决方案更灵活,但我不确定这是否可行.谢谢!

Mar*_*k H 6

var c = (BaseClass)Activator.CreateInstance(dict["type1"]);
Run Code Online (Sandbox Code Playgroud)