使用一个字符串值来创建新的表单对象 [英] Use value of a string to create new form object
问题描述
我有一个推出的主要形式,然后就可以去任何我所创建的其他形式。但踢球的是,我写了一个类,我调用返回的形式去的名称的字符串。
I have a main form that is launched and then it can go to any of the other forms I have created. But the kicker is that I have written a class that I call that returns a string with the name of the form to go to.
目前我没有这方面的工作所以我从形式要形成这样的(静态链接编写代码):
Currently I don't have this working so I am going from form to form like this (statically written linking code):
this.Hide();
CloudAccess nextForm1 = new CloudAccess();
//Where CloudAccess is the class of the next form.
nextForm1.ShowDialog();
我要的是这样的:
What I want is something like this:
FormController pick = new FormController();
//Where FormController is the Class I create an object of and ask what's next
string next = pick.whereToGo(); //lets say it returns "CloudAccess"
this.Hide();
next nextForm1 = new next(); //next is desired to be the contents of the string
nextForm1.ShowDialog();
的问题是,我不知道如何使用返回的字符串,使新的对象,用它。我一直在寻找的调用和思考的话题像这样的:使用字符串值创建新实例
,而我是新的C#,我不知道如何适用于这种情况。
The problem is that I don't know how to use the returned string to make the new object and use it. I've been looking at Invoke and Reflection topics like this one: Use string value to create new instance But I'm new to C# and I'm not sure how to apply that to this scenario.
的思考? !谢谢
推荐答案
下面是从工作的代码是什么fejejosco说:
Here's the working code from what fejejosco said:
string asdf = "CloudAccess";
Type CAType = Type.GetType("namespace." + asdf);
Form nextForm2 = (Form)Activator.CreateInstance(CAType);
nextForm2.ShowDialog();
谢谢!
Thanks!
这篇关于使用一个字符串值来创建新的表单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!