1、類庫項目引用System.Windows.Forms並添加引用后,才可創建窗體。
2、控制台應用程序調用中間庫(DLL)中的方法創建窗體;中間類庫使用反射下的Assembly加載包含窗體的類庫及創建實例。
注意:1)創建實例時,參數為窗體類的全名(命名空間+類名)。
2)返回值是Object類型,需轉化為Form類型。
3)exe(控制台程序)、中間類庫(dll)、窗體所在類庫(dll)在同一目錄下。
4)Load(空間名,窗體類全名),LoadFrom(*.dll,窗體類全名)
具體代碼如下:
窗體類庫:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TestForm { public partial class Test : Form { public Test() { InitializeComponent(); } } }
中間類庫:
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Anxin.Factory { public class FormFactory { /// <summary> /// 創建窗體 /// </summary> /// <param name="val_classFullName"></param> /// <returns></returns> public static Form CreateForm(string val_classFullName) { string nameSpace = val_classFullName.Substring(0,val_classFullName.LastIndexOf('.')); return (Form)Assembly.Load(nameSpace).CreateInstance(val_classFullName); } /// <summary> /// 創建窗體 /// </summary> /// <param name="val_nameSpace">命名空間</param> /// <param name="val_className">窗體類名</param> /// <returns></returns> public static Form CreateForm(string val_nameSpace, string val_className) { //string className = val_className; //僅使用類名創建示例失敗。 string classFullName = string.Format("{0}.{1}",val_nameSpace,val_className); //MessageBox.Show(Assembly.GetExecutingAssembly().Location); return (Form)Assembly.Load(val_nameSpace).CreateInstance(classFullName); } public static Form CreateForm(string val_formAssemblyFile, string val_formFullName, Object[] val_formArgs) { return CreateForm(val_formAssemblyFile, val_formFullName, val_formArgs, null); } public static Form CreateForm(string val_formAssemblyFile,string val_formFullName,Object[] val_formArgs,string val_formText) { Form form; string assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string assemblyFullName = assemblyPath + val_formAssemblyFile; if (!assemblyPath.EndsWith("\\")) { assemblyFullName = assemblyPath + "\\" + val_formAssemblyFile; } //MessageBox.Show(assemblyFullName); Assembly formAssembly = Assembly.LoadFrom(assemblyFullName); form = formAssembly.CreateInstance(val_formFullName) as Form; if(null == form) { string strError = string.Format("創建窗體失敗!\n程序集{0}\n窗體類名稱{1}", val_formAssemblyFile, val_formFullName); if (!string.IsNullOrEmpty(val_formText)) { strError += "窗體標題" + val_formText; } throw new Exception(strError); } if (!string.IsNullOrEmpty(val_formText)) { form.Text = val_formText; } return form; } } }
控制台程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Anxin.Factory; using System.Windows.Forms; namespace TestFactory { class Program { static void Main(string[] args) { string nameSpace = "TestForm"; string className = "Test"; //窗體類名 //string className = "Example"; //窗體文件名 (報錯) Form testForm = FormFactory.CreateForm(nameSpace,className); string classFullName = nameSpace + "." + className; testForm = FormFactory.CreateForm(classFullName); string dllName = "TestForm.dll"; //string classFullName = "TestForm.Test"; testForm = FormFactory.CreateForm(dllName,classFullName,null); string formText = "OK 加油!"; testForm = FormFactory.CreateForm(dllName, classFullName, null,formText); if (null != testForm) { testForm.ShowDialog(); } else { Console.WriteLine("通過調用Dll,創建窗體失敗!\n"); //使用窗體文件名創建時失敗! } } } }