c# - 如何将数据类型(string)转换为类型(form)

标签 c# winforms

我有一个存储在字符串变量中的表单名称,我想将这个字符串传递给采用表单类型的类

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/

相关文章:

c# - 在 EF 5.0 中链接 OR 条件

c# - 为什么 HashSet<T> 没有实现 IReadOnlyCollection<T>?

c# - IIS 部署的 ASP.NET Core 应用程序提供间歇性 431 请求 header 太长错误

c# - 让 SetDesktopBounds 坚持下去

如果设置了背景图像,加载时 Winforms 应用程序会抖动

c# - 单击选项卡控件的选定选项卡页眉时如何触发事件

c# - 在 XAML 中使用单例 - 可能吗?

c# - W3C (IIS) 日志记录不记录我的 WCF 服务的 ClaimsIdentity 用户名

c# - 无法访问已处置的对象。对象名称 : 'NumericUpDown'

.net - WM_DESTROY、WM_CLOSE绕过IMessageFilter