Dynamic DLL loading (C #)

Source: Internet
Author: User
Public Class Plugingmanager
{
// Plug-in loader
Public Arraylist plugins = New Arraylist ();
// Plug-in fullname
Public Arraylist plugfullname = New Arraylist ();
// Plug-in type
Public Arraylist plugtypes = New Arraylist ();

# Region Constructor
///   <Summary>
/// Plugingmanager plug-in loading
///   </Summary>
///   <Param name = "plugspath"> The directory where the plug-in is located must be a folder in the running directory. </Param>
///   <Param name = "startswith"> Load the specified plug-in (name included in the plug-in) </Param>
///   <Param name = "interfacename"> Plug-in Interface Name </Param>
Public Plugingmanager ( String Plugspath, String Startswith, String Interfacename)
{
// Obtain all files under the plug-in directory (plugins)
String [] Files = directory. getfiles (application. startuppath + @" \\ " + Plugspath );

Foreach ( String File In Files)
{
If (File. toupper (). endswith (startswith. toupper ()))
{
Try
{
// Assembly AB = assembly. loadfrom (File );
Assembly AB = Null ;

// First, copy the plug-in to the memory buffer.
Byte [] Addinstream =Null ;
Using (Filestream input = New Filestream (file, filemode. Open, fileaccess. Read, fileshare. Read ))
{
Binaryreader reader = New Binaryreader (input );
Addinstream = reader. readbytes (( Int ) Input. Length );

}
AB = assembly. Load (addinstream ); // Load DLL in memory
Type [] types = AB. gettypes ();
Foreach (Type T In Types)
{
If (T. getinterface (interfacename )! = Null )
{
Plugins. Add (AB. createinstance (T. fullname ));
Plugfullname. Add (T. fullname );
}
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
}
}
# Endregion
} Plugingmanager plug = New Plugingmanager (" Plugs " , " Garden. plugs. dll " , " Iplug " );

VaR Win = plug. plugins. toarray (). firstordefault (M => (type) M. GetType (). Name. tolower () = This . Tag. tostring (). tolower ());
Methodinfo onshowdlg = (type) win. GetType (). getmethod ( " Showself " );
Form CL = (form) onshowdlg. Invoke (win, Null );
Cl. windowstate = formwindowstate. maximized;
Cl. mdiparent = This ;
Cl. Show ();
Foreach ( Object OBJ In Plug. plugins)
{

Type T = obj. GetType ();
Methodinfo onshowdlg = T. getmethod ("Showself");
Control Cl = (Control) onshowdlg. Invoke (OBJ,Null);

Control con = getcontrolfromform (T. Name, This );
If (Con! = Null )
{
Con. Controls. Add (CL );
Cl. Dock = dockstyle. Fill;
Isbreak = False ;
Con = Null ;
}

}

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.