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 ;
}
}