Prior to the use of the prism framework, the Extensibility Framework MEF (Managed Extensibility Framework) was exposed to experience the great convenience and scalability that MEF brings.
This article will write a composable application that can help you quickly familiarize yourself with MEF and apply it to real projects.
For the meaning of nouns and the realization of function in MEF, please take a seat: train tickets
Introduce the demo program () that will be written, using WinForm development.
- By combining operations, the program dynamically loads the available parts for combined operation.
- The program unloads all the loaded parts through the disassembly operation.
To reference an assembly after you create a new project:
System.ComponentModel.Composition
The core code of the main program is as follows:
Public partial class Form1:form, ipartimportssatisfiednotification {[ImportMany (allowrecomposition = True)] Private Ienumerable<lazy<iplugin, ipluginmetadata>> plugins; Private Aggregatecatalog Catalog; Private Compositioncontainer container; Public Form1 () {InitializeComponent (); if (catalog = = null) Catalog = new Aggregatecatalog (); This.container = new Compositioncontainer (catalog); This.container.ComposeParts (this); } #region implementation of ipartimportssatisfiednotification public void onimportssatisfied () { FlowLayoutPanel1.Controls.Clear (); if (plugins! = null && plugins. Count ()! = 0) {plugins. ToList (). ForEach ((a) = {button btn = new Button (); Btn. Cursor = System.Windows.Forms.Cursors.Hand; Btn. Width = 100; Btn. Height = 50; Btn. Text = A.metadata.thepluginname; Btn. Click + = (d, b) = = {A.value.run ();}; FLOWLAYOUTPANEL1.CONTROLS.ADD (BTN); }); }} #endregion public void Compositionaction () {catalog. Catalogs.add (New Assemblycatalog (assembly.getexecutingassembly ())); } ...... }
1. IPartImportsSatisfiedNotification interface: After the component import is complete, call the method (Onimportssatisfied) in the interface.
2. The most commonly used directories in MEF are three: assembly directory (Assemblycatalog), file directory (Directorycatalog), aggregate directory (Aggregatecatalog)
Assembly Directory (Assemblycatalog): As the name implies, you can add an assembly to a directory that already exists in the type to look for parts that can be imported.
var catalog = new Assemblycatalog (System.Reflection.Assembly.GetExecutingAssembly ());
Folder directory (Directorycatalog): Find parts from folders that are available for import
var catalog = new Directorycatalog ("Extensions");
Aggregation directory (Aggregatecatalog): You can include both of these methods in the aggregation catalog
var catalog =new Aggregatecatalog ( new Assemblycatalog (System.Reflection.Assembly.GetExecutingAssembly ()), new Directorycatalog ("Extensions"));
3. Compositioncontainer: Combine containers, manage component combinations and provide a range of methods for creating part instance extensions, etc., detailed information
4. General use of directories and containers:
var catalog =new aggregatecatalog (); var container =new compositioncontainer (catalog); var container.composeparts (this);
5. Import: Importattribute and Importmanyattribute
For the following three uses: Fields, properties, methods
[Import]private IData _data; [Import]public IData Data{set;get;} [Import]public Action clickaction;
Importmanyattribute: Populating all contract-compliant exports by combining containers (it's awkward, just to import multiple)
[Importmany]private ienumerable<iplugin> plugins;</iplugin>
Allowrecomposition: Whether to allow reorganization
Allowrecomposition = true: For example, when the program is running, dynamically adding exportable parts to the aggregated directory can trigger a reorganization operation
Catalog. Catalogs.add (New Assemblycatalog (assembly.getexecutingassembly ()));
It is important to note that Directorycatalog does not cause a reorganization operation and can be specified by the Refresh method.
6. System.lazy<t>: MEF provides deferred import.
Let's take a look at how plug-ins are implemented:
[Exportpluginattribute (typeof (IPlugin), thepluginname = "Thefristplugin")] public class Thefristplugin:iplugin {public thefristplugin () } {this . thename = "Thefristplugin"; } #region implementation of IPlugin public string Thename {get; set;} public void Run () { MessageBox.Show ("Thefristplugin"); } #endregion }
1. Simply say: the relationship before import and export
An extensible program based on the MEF development, there must be a lot of export in the container, and these export how to find their own destination.
Export and Import rely on a contract to determine whether the other party is his or her brother, which is plainly an interface, such as the IPlugin interface defined by the above program
Public interface IPlugin { void Run ();
Export using the Exportattribute attribute:
[Export ("Count")] public int count{Get{return 0;}} [Export (typeof (Action)]) public void sendmsg () {return;}
There is a requirement that the main program requires that the plug-in must specify the plug-in name:
1. Defined in the IPlugin interface: Name field
2. Using Meta data
3. Using the Custom export attribute (similar to the second scenario)
How do I use metadata?
1. Define the metadata view, where the view uses the interface type
Public interface Ipluginmetadata { string thepluginname {get;}}
2. When exporting a part, use the ExportMetaData feature
[ExportMetaData ("Thepluginname", "Thefiveplugin")] [Export (typeof (Mef.test.wform.Interface.IPlugin))]public class thefiveplugin:mef.test.wform.interface.iplugin{ Public void Run () { MessageBox.Show ("Thefiveplugin");} }
3. Import meta-data
[ImportMany (Allowrecomposition = True)]private ienumerable<lazy<iplugin, ipluginmetadata>> plugins;
4. Accessing meta data
Lazy<t,tmetadata>. Value.metadata
End
So far, the basic content of MEF has been explained to the end, if there are omissions, please note that Bo friends.
Many of the articles are vernacular, non-official language, how to understand how to write, if there is inappropriate, but also hope that Bo friends pointed out.
Happy new year
[. NET] talking about extensibility framework: MEF