C # Plugin practices

Source: Internet
Author: User

Plugin is a flexible and scalable modular mechanism. Many Application blocks of Microsoft use the Plugin structure. This article discusses a kind of practice of Plugin in C.
Plugin is an object that implements IPlugin using the Reflection mechanism in C # based on a unified interface. If the interface is IPlugin, the following code block indicates the code segment that dynamically generates the IPlugin object. dllName indicates the dll file name that implements the IPlugin interface class:

Public IPlugin LoadPlugin (string dllName)
{
Assembly asm = AppDomain. CurrentDomain. Load (dllName );
Foreach (Type t in asm. GetTypes ()){
Foreach (Type iface in t. GetInterfaces ()){
If (iface. Equals (typeof (IPlugin ))){
Return (System. Activator. CreateInstance (t) as IPlugin;
}
}
}
Return null;
}
 
Java also has a Reflection mechanism to implement Plugin, which is roughly like (IPlugin) Class. forName (className). newInstance. C ++ can use DLL simulation because of the Reflection mechanism. In the DLL, a global function and interface are export, and the global function returns the pointer of the object that implements the interface.

Microsoft has an article on Provider Pattern. In this article, another C # instantiation method with parameters is provided.

Related Article

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.