直接调用
string strName = "MyOpsTools." + "stringFormName";//要调用窗体的 程序集.+窗体名称 (要有.)
string assemblyPath = "程序集名称的长格式";
Form form = (Form)Assembly.Load(assemblyPath).CreateInstance(strName)
form.show()//显示窗体
2,传参调用窗体
前面都一样,继续使用Assembly的CreateInstance方法
//
public object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);
通过查看程序源码 然后针对调用的窗口的构造函数进行,对args赋值,
// args:
// Object 类型的数组,包含要传递给构造函数的参数。此参数数组在数量、顺序和类型方面必须与要调用的构造函数的参数匹配。如果需要默认的构造函数,则 args
// 必须是空数组或 null。
object [] arg =new object[1];
string pra = "111";
arg[0] = pra;
string strName = "MyOpsTools." + stringFormName;
string assemblyPath = path;
Form form = (Form)Assembly.Load(assemblyPath).CreateInstance(strName, false, BindingFlags.Default, null, arg, null, null);
//之后修改窗体构造参数
public Frm_test(string arg):this()
{
MessageBox.Show(arg);
}
//参数个数为1.类型为string
//结果窗体成功调用,参数成功赋值
当有多个参数时,只要将传参的 object [] arg 进行更改跟构造函数一直即可调用成功,否则会提示:
“System.MissingMethodException”类型的未经处理的异常在 mscorlib.dll 中发生
其他信息: Constructor on type 'MyOpsTools.Frm_test' not found.