C # reflection to implement a Simple plug-in system

Source: Internet
Author: User

If you have used some excellent open-source or non-open-source application software, you will find that a major advantage of these software is its openness, anyone with the ability can develop different plug-ins for them to enhance their functions. For example, the famous foobar2000, Vim and TotalCommander.

C # reflection can be used to implement a Simple plug-in system. The idea is simple. We create a solution that contains three projects, one for our software MyApplication, the other for the plug-in interface IPlugin, and the other for the specific plug-in MyPlugin. The basic idea of the plug-in system is to use an interface class library to define the method signature that must be implemented by the plug-in that can be used by our software. Then, our software MyApplication dynamically calls by referencing the IPlugin dll, and the specific implementation plug-in MyPlugin references this interface to implement specific methods. In this way, our application can call the plug-in without knowing the specific plug-in ..

The structure is as follows:

The key code is load the assembly. Find the implementation class of the corresponding interface. Then you can call it.


Assembly a = Assembly. LoadFrom (AssemblyName); foreach (Type t in a. GetTypes () {if (t. GetInterface ("IMyFunction ")! = Null) {try {IMyFunction pluginclass = Activator. createInstance (t) as IMyFunction; pluginclass. doSomething ();} catch (Exception ex) {MessageBox. show (ex. toString ());}}}

 

Running result:

Source code download: PluginSystem.zip

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.