[. NET] talking about the extensibility framework: MEF
Previously, when using the Prism Framework, we were exposed to the scalability Framework MEF (Managed Extensibility Framework) and experienced the great convenience and scalability brought by MEF. This article will compile an application that can be combined to help you quickly familiarize yourself with MEF and apply it to actual projects. For the glossary and function implementation in MEF, please go to: train ticket introduction to the Demo program () to be compiled and use winform for development. Through the combination operation, the program dynamically loads available parts for the combination operation. By means of dismounting, the program uninstalls all the loaded parts. After creating a project, you must reference the Assembly: System. componentModel. the core code of the Composition main program is: 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 (); t His. 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 =. metadata. thePluginName; btn. click + = (d, B) => {. value. run () ;}; flowLayoutPanel1.Controls. add (btn) ;}}# endregion public void CompositionAction () {catalog. catalogs. add (new AssemblyCatalog (Assembly. getExecutingAssembly ()));}.... ..} 1. IPartImportsSatisfiedNotification interface: Call OnImportsSatisfied after the component is imported ). 2. MEF has three common directories: AssemblyCatalog, DirectoryCatalog, and AggregateCatalog. AssemblyCatalog ): as the name suggests, you can add an existing assembly type to the directory to find the parts that can be imported. Var catalog = new AssemblyCatalog (System. reflection. assembly. getExecutingAssembly (); directory (DirectoryCatalog): Find the component that can be imported from the folder var catalog = new DirectoryCatalog ("Extensions"); Aggregate directory (AggregateCatalog ): you can include the preceding two methods to the aggregation Directory: var catalog = new AggregateCatalog (new AssemblyCatalog (System. reflection. assembly. getExecutingAssembly (), new DirectoryCatalog ("Extensions"); 3. compositionContainer: combines containers, manages components, and provides A series of extension methods for creating part instances. For details, see section 4. common usage of directories and containers: var catalog = new AggregateCatalog (); var container = new CompositionContainer (catalog); container. composeParts (this); 5. import: ImportAttribute and ImportManyAttribute are used for the following purposes: field, attribute, method [import] private IData _ data; [import] public IData Data {set; get ;} [import] public Action ClickAction; ImportManyAttribute Enumerable <IPlugin> plugins; AllowRecomposition: whether to allow the reorganization of AllowRecomposition = true: for example, when a program is running, dynamically adding the exported parts to the aggregation directory can cause the reorganization operation catalog. catalogs. add (new AssemblyCatalog (Assembly. getExecutingAssembly (); note that DirectoryCatalog does not trigger a reorganization operation. You can use the Refresh method to specify the reorganization policy. 6. System. Lazy <T>: MEF provides delayed import. Next let's take a look at how the plug-in is 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. to put it simply, the relationship between import and Export is an extensible program developed based on MEF. There must be a lot of Export (exports) in the container, and these exports How can we find our own destination. Export and Import depend on a contract to determine whether the other party is a brother. To put it bluntly, it is an interface. For example, the IPlugin interface defined by the above program is public interface IPlugin {void Run ();} use the ExportAttribute feature to Export: [Export ("count")] public int count {get {return 0;} [Export (typeof (Action)] public void SendMsg () {return;} [Export] public class Person {} has a requirement. The main program requires that the plug-in must specify the plug-in name: 1. define the Name field 2 in the IPlugin interface. use metadata 3. how do I use metadata using the custom export feature (similar to the second solution? 1. define the metadata view. The view uses the interface type public interface IPluginMetadata {string ThePluginName {get ;}} 2. when exporting parts, 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") ;}} copy code 3. import metadata [ImportMany (AllowRecomposition = true)] private IEnume Rable <Lazy <IPlugin, IPluginMetadata> plugins; 4. access metadata Lazy <T, TMetadata>. value. metadata has ended. The basic MEF content has ended. If any omission exists, ask Boyou to leave a message. Many of the articles are in Vernacular and non-official languages. If there is anything wrong, I hope you can point out it.