我有一个存储在字符串变量中的表单名称,我想将这个字符串传递给采用表单类型的类
string Str = "MainForm"; // this is form name
// I try to convert the data type string to form type like this
Form FormNm = (Form) Str ;
// there is the message appears cannot convert type 'sting' to 'form'
TransLang.SaveControlsLanguage(FormNm); // this is the class
谢谢
最佳答案
string myType = "MyAssembly.MyType";
Assembly asm = Assembly.LoadFrom("MyAssembly.dll"); // creating instance by loading from other assembly
//Assembly asm = Assembly.GetExecutingAssembly(); // for creating instance from same assembly
//Assembly asm = Assembly.GetEntryAssembly(); // for creating instance from entry assembly
Type t = asm.GetType(myType);
Object obj = Activator.CreateInstance(t, null, null);
因为它是一个 System.Windows.Form 如果你想显示你可以做的表单
Form frm = obj as Form; // safely typecast
if (frm != null) // kewl it is of type Form
{
//work with your form
fmr.Show();
}
关于c# - 如何将数据类型(string)转换为类型(form),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14412652/