1. 添加Reflection
1 //添加对Reflection程序集引用 2 using System.Reflection; 3 4 // 引用窗体创建方法CreateForm,传入参数 5 private void btnCreateForm_Click(object sender, EventArgs e) 6 { 7 CreateForm("testForms", "UseAssemblyToCreateForms"); 8 } 9 /// <summary> 10 /// 1.stringFormName,要创建的窗体名称 11 /// 2.path,引用程序集名称 12 /// 3.Assembly.Load(path),该方法加载程序集 13 /// 4.Assembly.Load(path).CreateInstance(strName),通过传入窗体名称创建窗体,窗体名称需为 :程序集路径.窗体名称 14 /// </summary> 15 /// <param name="stringFormName"></param> 16 /// <param name="path"></param> 17 private void CreateForm(string stringFormName,string path) 18 { 19 string strName = "UseAssemblyToCreateForms.UI." + stringFormName; 20 string assemblyPath = path; 21 22 Form form =(Form) Assembly.Load(assemblyPath).CreateInstance(strName); 23 form.Show(); 24 }
引用
2. 利用Assembly动态创建窗体