Three assemblies:
Main assembly: BaseApp.exe
Interface assembly: Ibaseapplication
Plug-in assemblies: Testattri
=======================================================================================
In the interface program:
Interface: IAPP
Attribute definition: Moduleattribute
Copy Code code as follows:
public interface iapp:imothed
{
void ParentForm (IApp frm);
}
Namespace Ibaseapplication.attributes
{
[AttributeUsage (attributetargets.assembly | Attributetargets.interface | Attributetargets.field | AttributeTargets.Method | AttributeTargets.Class | Attributetargets.property, AllowMultiple = true, inherited = False)]
public class Moduleattribute:attribute
{
public string Idname {get; set;}
public string ModuleName {get; set;}
Public Type Moduletype {get; set;}
public string Asmname {get; set;}
public string ClassName {get; set;}
public string Description {get; set;}
}
}
In the plug-in assembly:
The following is identified in the AssemblyInfo class in the plug-in assembly
Copy Code code as follows:
[Assembly:IBaseApplication.Attributes.Module (Moduletype = typeof (UserControl1), idname = " Be4d9a5b-0455-4e9d-a255-25122b80bef1-usercontrol1 ", ModuleName =" UserControl1 ", Description =" ")]
[Assembly:IBaseApplication.Attributes.Module (Moduletype = typeof (UserControl2), idname = " Be4d9a5b-0455-4e9d-a255-25122b80bef1-usercontrol2 ", ModuleName =" UserControl2 ", Description =" ")]
There are two modules, respectively, of the following
Copy Code code as follows:
Namespace Testattri
{
Public partial class Usercontrol1:usercontrol, IAPP
{
......
}
}
Namespace Testattri
{
Public partial class Usercontrol2:usercontrol, IAPP
{
......
}
}
=================================================================================================
In the main assembly:
Put the plugin to: Application.startuppath + "\\Plus"
Interface assembly "Ibaseapplication" referenced
Copy Code code as follows:
<summary>
Get plug-in file name
</summary>
<returns></returns>
Public string[] Getplusfiles ()
{
Return System.IO.Directory.GetFiles (Application.startuppath + "\\Plus");
}
<summary>
Loading Plug-ins
</summary>
public void Loadplufiles ()
{
string[] files = getplusfiles ();
Assembly Assembly = assembly.getcallingassembly ();
foreach (string file in files)
{
moduleattribute[] attributes = assembly.loadfile (file). GetCustomAttributes (typeof (Moduleattribute), false) as moduleattribute[];
foreach (Moduleattribute attribute in attributes)
{
string m = attribute. Moduletype.fullname;
String M1 = attribute. ModuleType.Assembly.GetName (). Name;
Object obj = Activator.CreateInstance (attribute. Moduletype);
if (obj is IApp)
{//the interface for two modules is not recognized.
}
}
}
}