我有一个向下转换的问题,如何将字符串“标签”转换为类类型标签进行转换,是否可以到达“文本”类?
public class GFXObject
{
// Constructor etc...
}
public class Label : GFXObject
{
public Texts Text = new TextClass();
// Constructor etc...
}
public class Button : GFXObject
{
// Constructor etc...
}
public class Texts
{
public string Text = "empty";
// Constructor etc...
}
// My List of objects
Dictionary<string, GFXObject> objects;
// Disregarding any troubleshooting
public void ChangeText(string classtype, string name, string text)
{
// How do I convert the string classtype to a ClassType
((classtype)objects[name]).Text.Text = text;
// If parameters would be equal to "Label", "label", "changed":
// ((Label)objects["label"].Text.Text = "changed";
}
如果扩展或修改,是否可以使用 ChangeText 函数?