You can store type information with a Type object:
var dict = new Dictionary<String, Type>();
dict.Add("A", typeof(TextBox));
dict.Add("B", typeof(Button));
and create objects from it like this:
object a = Activator.CreateInstance(dict["A"]);
This will only work with types with a parameterless constructor. For instance, new TextBox()
. If your types have constructors that take the same arguments, you can add the arguments after dict["A"]
or pass an array.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…