C # Questions about reflection loading _c# tutorial

Source: Internet
Author: User
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.
}
}
}
}

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.