0

我一直在玩弄 Windows 窗体,而且我对 C# 还是很陌生。所以你通常像下面这样声明一个实例:Button example = new Button();但是,如果类 Button 在命名空间内,你必须声明完全限定路径:foo.bar.Button newButton = foo.bar.Button();只要你知道要实现的每个完全限定路径,它就可以很好地工作。但是,我偶然发现了从字符串中获取完整限定路径的情况,因此我不确定如何声明该类的新实例。

private void treeLoader_Click(object sender, EventArgs e)
{
        string viewPath = this.tree.SelectedNode.FullPath;
        viewPath = viewPath.Replace(" ", "");
        viewPath = viewPath.Replace("\\", ".");
        // At this point viewPath equals "foo.bar.Button"

        Type newButton = Type.GetType(viewPath); // Failed attempt
}
4

0 回答 0