1. A class library project can create a form only after it references System.Windows.Forms and adds a reference.
2. The console application calls the method in the intermediate library (DLL) to create the form; the Intermediate class library loads the class library containing the form and creates the instance using the assembly under reflection.
Note: 1) When you create an instance, the parameter is the full name of the form class (Namespace + class name).
2) The return value is of type object and needs to be converted to form type.
3) EXE (console program), Intermediate Class library (DLL), the form's class library (DLL) in the same directory.
4) Load (Space name, form class full name), LoadFrom (*.dll, Form class full name)
The specific code is as follows:
Form Class Library:
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 ();}} }
Intermediate Class Library:
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>///Create Form///</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>//Create Form///</summary>//<param name= "Val_namespace" > Namespace </param>//<param name= "Val_classname" > Form class name </param>//<returns></returns> public static Form CreateForm (String val_namespace, String val_classname) { string className = Val_classname; Creating the sample with the class name only fails. 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 VA L_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 ("Create form failed!\n assembly {0}\n Form class name {1}", Val_formassemblyfile, Val_formfullname); if (!string. IsNullOrEmpty (Val_formtext)) {strerror + = "form title" + Val_formtext; } throw new Exception (strerror); } if (!string. IsNullOrEmpty (Val_formtext)) {form. Text = Val_formtext; } return form; } }}
Console program:
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"; Form class name//string ClassName = "Example"; Form file name (error) 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, come on!" "; Testform = Formfactory.createform (DllName, Classfullname, Null,formtext); if (null! = Testform) {testform.showdialog (); } else {Console.WriteLine ("Failed to create form by calling DLL!"} \ n "); Failed to create with form file name! } } }}
C #: Console program calls Intermediate library create form