[Go] load a form dynamically from a DLL assembly

Source: Internet
Author: User

Infinite source dynamically loading forms from DLL assemblies [original]

It took me a night last night to write a program that dynamically loads a form from an assembly. Compile any code containing the form into a DLL file, and then copy the DLL file to the directory of this program, the program can be dynamically checked into the DLL file in the form, the type of the form class in the Program menu display, click the menu to run the corresponding form.


This program mainly uses the Assembly class dynamic loading assembly, and then gets the type of class contained in the assembly, dynamic generation of class instances, dynamic invocation of class methods. Personally feel that this is a highly loose coupling, can be arbitrarily extended program structure framework, I hope to discuss with you the application of this framework of the future!

The key codes are as follows:

usingSystem;usingSystem.Drawing;usingSystem.IO;usingSystem.Reflection;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;namespacewindowsformtest{/// <summary>    ///a summary description of the Form1. /// </summary>     Public classFrmMain:System.Windows.Forms.Form {Private intFormnum; PrivateArrayList formtypes =NewArrayList (); PrivateArrayList formobjects =NewArrayList (); PrivateSystem.Windows.Forms.MainMenu Mnumain; PrivateSystem.Windows.Forms.MenuItem Mnuitmrun; PrivateSystem.Windows.Forms.MenuItem Mnuitemwindow; PrivateSystem.Windows.Forms.MenuItem Mnuitmhelp; PrivateSystem.Windows.Forms.MenuItem Mnuitmcascade; PrivateSystem.Windows.Forms.MenuItem mnuitmtilehorizontal; PrivateSystem.Windows.Forms.MenuItem mnuitmtilevertical; PrivateSystem.Windows.Forms.MenuItem menuItem1; PrivateSystem.Windows.Forms.MenuItem mnuitmabout; PrivateSystem.Windows.Forms.StatusBar Stabarmain; PrivateSystem.Windows.Forms.StatusBarPanel Pnlinfo; PrivateSystem.Windows.Forms.StatusBarPanel Pnlnum; /// <summary>        ///the required designer variables. /// </summary>        PrivateSystem.ComponentModel.Container components =NULL;  Publicfrmmain () {//            //The Windows Forms Designer supports the required//InitializeComponent (); //            //TODO: Add any constructor code after the InitializeComponent call//        }        /// <summary>        ///clean up all the resources that are in use. /// </summary>        protected Override voidDispose (BOOLdisposing) {            if(disposing) {if(Components! =NULL) {components.                Dispose (); }            }            Base.        Dispose (disposing); The code generated by the Windows Forms DesignerPrivate voidFrmmain_load (Objectsender, System.EventArgs e) {Assembly Assembly=NULL; stringWindowspath = Path.Combine (Application.startuppath,"Windows"); foreach(stringDllfileinchDirectory.GetFiles (Windowspath,"*.dll") ) {Assembly=Assembly.loadfile (Dllfile); Type[] Types=Assembly.                                GetTypes (); foreach(Type tinchtypes) {                    if(T.basetype = =typeof(Form)) {                         This. Formtypes.add (t); MenuItem Item= This. MNUITMRUN.MENUITEMS.ADD (T.fullname); Item. Click+=NewEventHandler (Menuitemnewitem_click); }                }            }        }        Private voidMenuitemnewitem_click (Objectsender, EventArgs e) {MenuItem Item=(MenuItem) sender; Type T= (Type) ( This. Formtypes[item.            Index]); Object obj=activator.createinstance (t);  This. Formobjects.add (obj); Formnum+=1; T.invokemember ("MdiParent", BindingFlags.SetProperty,NULLObjNew Object[] { This}); T.invokemember ("Text", BindingFlags.SetProperty,NULLObjNew Object[] {t.fullname+"form:"+formnum}); T.invokemember ("Show", BindingFlags.InvokeMethod,NULLObjNew Object [] {}); (Form), obj). Closing+=NewCancelEventHandler (frmwindow_closing); (Form), obj). Activated+=NewEventHandler (frmwindow_activated); MenuItem MenuItem= This. MNUITEMWINDOW.MENUITEMS.ADD ((Form) obj).            Text); MenuItem.Click+=NewEventHandler (Menuitemwindow_click);  This. Pnlnum.text ="currently loaded"+ This. formobjects.count+"a form";  This. Pnlinfo.text ="Current active form:"+ This.        Activemdichild.text; }        Private voidMenuitemwindow_click (Objectsender, System.EventArgs e) {MenuItem Item=(MenuItem) sender; (Form) ( This. Formobjects[item. index-4])).            Activate ();  This. Pnlnum.text ="currently loaded"+ This. formobjects.count+"a form";  This. Pnlinfo.text ="Current active form:"+ This.        Activemdichild.text; }        Private voidFrmwindow_activated (Objectsender, System.EventArgs e) {             This. Pnlnum.text ="currently loaded"+ This. formobjects.count+"a form";  This. Pnlinfo.text ="Current active form:"+ This.        Activemdichild.text; }        Private voidFrmwindow_closing (Objectsender, System.ComponentModel.CancelEventArgs e) {             for(intI=0; i< This. Formobjects.count; i++)            {                if((Form) This. Formobjects[i]). Text = =(Form) sender). Text) { This. Formobjects.removeat (i);  This. MnuItemWindow.MenuItems.RemoveAt (i+4);  This. Pnlnum.text ="currently loaded"+ This. formobjects.count+"a form";  This. Pnlinfo.text ="Current active form:"+ This.                    Activemdichild.text;  Break; }            }        }        Private voidMnuitmcascade_click (Objectsender, System.EventArgs e) {             This.        LayoutMDI (Mdilayout.cascade); }        Private voidMnuitmtilehorizontal_click (Objectsender, System.EventArgs e) {             This.        LayoutMDI (mdilayout.tilehorizontal); }        Private voidMnuitmtilevertical_click (Objectsender, System.EventArgs e) {             This.        LayoutMDI (mdilayout.tilevertical); }        Private voidMnuitmabout_click (Objectsender, System.EventArgs e) {            NewFrmabout (). ShowDialog ( This); }    }}

Download Program source code:/files/infinity/windowsform.rar


Program
When a single DLL file contains a form:

When a form class is included in all assemblies under the program directory, the program runs the interface:

When multiple DLL files contain multiple forms:

When the program directory contains multiple form classes in all assemblies, the program runs the interface:

[GO] dynamically load a form from a DLL assembly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.