Three Assembly:
Main Assembly: BaseApp.exe
Interface Assembly: IBaseApplication
Plug-in assembly: TestAttri
========================================================== ========================================================== ========
In the interface program:
Interface: IApp
Attribute definition: ModuleAttribute
Copy codeThe Code is as follows: public interface IApp: IMothed
{
Void ParentForm (IApp frm );
}
Namespace IBaseApplication. Attributes
{
[AttributeUsage (AttributeTargets. Assembly | AttributeTargets. Interface | AttributeTargets. Field | attributes. Method | attributes. 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 AssemblyInfo class in the plug-in Assembly identifies the followingCopy codeThe Code is 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:Copy codeThe Code is as follows: namespace TestAttri
{
Public partial class UserControl1: UserControl, IApp
{
......
}
}
Namespace TestAttri
{
Public partial class UserControl2: UserControl, IApp
{
......
}
}
========================================================== ========================================================== ========================
In the main assembly:
Add the plug-in to: Application. StartupPath + "\ Plus"
The interface assembly "IBaseApplication" is referenced"Copy codeThe Code is as follows: // <summary>
/// Obtain the plug-in file name
/// </Summary>
/// <Returns> </returns>
Public string [] GetPlusFiles ()
{
Return System. IO. Directory. GetFiles (Application. StartupPath + "\ Plus ");
}
/// <Summary>
/// Load the plug-in
/// </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 of the two modules cannot be identified.
}
}
}
}