C # reflection Loading

Source: Internet
Author: User

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

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.