I have a dictionary object
Dictionary<string, Type> dict = new Dictionary<string, Type>();
I also have a BaseClass and a number of child-classes, say: ChildClass1 , ChildClass2 , etc. The dictionary has values:
dict.Add("type1", typeof(ChildClass1));
dict.Add("type2", typeof(ChildClass2));
My question is - is there any way to do something like this:
BaseClass c = new <<get a type from the dict: dict[type1]>>()?
I just want to make my solution more flexible, but I'm not sure if that's possible at all. Thanks!
var c = (BaseClass)Activator.CreateInstance(dict["type1"]);
您可以调用Activator.CreateInstance(type)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.