Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the MEF plug-in in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the MEF plug-in in C#. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Introduction to MEF

MEF, full name Managed Extensibility Framework (managed Extensible Framework). From the name alone, it is not difficult to find that MEF is a framework dedicated to solving extensibility problems, and there is a description of MEF in MSDN:

Managed Extensibility Framework or MEF is a library for creating extensible lightweight applications. Application developers can use the library to discover and use extensions without having to configure them. Extension developers can also use the library to easily encapsulate code and avoid generating fragile hard dependencies. With MEF, you can reuse extensions not only within applications, but also between applications.

Start coding

Let's create a new desktop application project called MefDemo

Add a MenuStrip under Form1 and change its name to ms

There is no need to add any items, we will load dynamically at that time

Right-click to add a new project selection class library in the solution

This class library is used to implement the interface of the plug-in

After building it, add a class to it and call it IPlugin

The interface is defined, one of which is the string type Text, and the other is DO;, and then we can inherit the interface when we add the insert.

Then go back to the Form in the main program to declare our interface

Then we write the form to call the initialization method of loading the plug-in

The method that is loaded into the MenuStrip control after reading the plug-in for display

Initialize Init () directly in the constructor, so that it will be loaded into the control in Form_Load and displayed

The code for the main form is complete.

Plug-in writing

We will create three new class libraries in the solution, so don't call it Plugin1,Plugin2,Plugin3.

Plugin1

The Text and Do () in the plug-in are the corresponding methods to implement the interface. We define it as plug-in 1 in plugin1, and the DO method implements the pop-up form to display plug-in 1 in Chinese.

Plugin2

Same as plug-in 1, but with a different name

Plugin3

Plug-in 3 adds a form Form1 in addition to the implemented interface class

Then we write the implementation method of the interface in MyPlugin3.

You can see that Form1 is directly displayed in Do.

A Button has been added to Form1.

The event of Button is that the pop-up form shows that I am plug-in 3

In this way, the code is implemented. After compilation, we put the DLL of the three plug-ins under plugin in the running directory.

On how to use the MEF plug-in in C# to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report