An example of MEF

Source: Internet
Author: User

MEF (Managed extensibility Framework). NET framework4.0 Join,

The biggest difference between MEF and MAF (Managed Addin Framework) is that the former focuses on using a very simple way to support scalable support with strong flexibility, which focuses on plug-in platform architectures with physical isolation, security, and multi-versioning support;

The difference between MEF and unity is that the former emphasizes the combination and the latter emphasizes dependency injection.

namespace interfaces{    publicinterface  IPlugin    {       void  Write () ;    }}
namespace oneplugin{[Export (typeof(IPlugin))]    publicclass  Oneplugin : IPlugin    {        publicvoid  Write ()        {            Console.WriteLine (  "oneplugin");     }}}
namespace twoplugin{    [Export (typeof (IPlugin))] public    class Twoplugin:iplugin    {public        void Write ()        {            Console.WriteLine ("Plugin");}        }    }

  

namespacemefsample{classProgram {Static voidMain (string[] args) {            //plug-in implementation using reflection//ienumerable<type> plugintypes = Getplugintypes (); //foreach (type type in plugintypes)//{            //IPlugin plugin = (IPlugin) activator.createinstance (type); //plugin.            Write (); //}PlugInProvider Provider =NewPlugInProvider (); foreach(Lazy <IPlugin> Plugininchprovider. Plugins) {plugin. Value.write ();        } console.read (); }        /// <summary>        ///plug-in implementation using reflection/// </summary>        /// <returns></returns>         Public StaticIenumerable<type>getplugintypes () {stringRoot =AppDomain.CurrentDomain.BaseDirectory; string[] files = directory.getfiles (root,"*.dll", searchoption.topdirectoryonly); List<Type> types =NewList<type>(); foreach(stringFileinchfiles) {type[] Typearray=assembly.loadfrom (file).                GetTypes (); foreach(Type typeinchtypearray) types.            ADD (type); }            return  fromTypeinchTypeswhereType. IsClass && (typeof(IPlugin). IsAssignableFrom (Type))Selecttype; //or return with yield .        }    }     Public classPlugInProvider {[ImportMany] PublicIenumerable<lazy<iplugin>> Plugins {Get;Private Set; } //[Import]//Public lazy<iplugin> Pluginone;         PublicPlugInProvider () {//var catalog = new Assemblycatalog (assembly.getexecutingassembly ());Aggregatecatalog Catalog =NewAggregatecatalog (); Catalog. Catalogs.add (NewDirectorycatalog (".")); varcontainer =Newcompositioncontainer (catalog); Container.composeparts ( This); }    }}

You can also name:
 Public Interfaceilogger{voidWritelog (stringmessage);} [Export ("TXT",typeof(ILogger))] Public classtxtlogger:ilogger{ Public voidWritelog (stringmessage)    {System.Console.WriteLine (message); }}[export ("DB",typeof(ILogger))] Public classdblogger:ilogger{ Public voidWritelog (stringmessage)    {System.Console.WriteLine (message); }}
[Export]  Public class facadeservice{    [Import ("TXT")]    publicget  set;}    [Import ("DB")]      Public Get Set ; }}

[Import]public facadeservice Service {get; set;}

  

 Public MainPage () {    InitializeComponent ();       var  as App). Service;    Service. Txtlogger.writelog (" write log to text file ");    Service. Dblogger.writelog (" write log to database ");} Copy Code

An example of MEF

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.