C# ------ MEF

reference

reference

MEF stands for Managed Extensibility Framework, is a library for creating scalable, lightweight application. Developers can use MEF to find and use extended, but does not need to be configured, but also can be reused extend between applications. Extended application is a problem of a large application architect must consider. Previous solutions usually use the configuration file, and the interface reflection technology to achieve. And MEF is provided a method of implicitly through the discovery component. MEF component specifying dependencies declaratively simultaneously (referred to as "import") provides its function (referred to as "export"). MEF is an integral part of the .NET Framework 4, the early version of the .NET Framework introduces Managed Add-in Framework (MAF) , designed to enable applications to isolate and manage extensions. MAF focus on a slightly higher level than the MEF, it focuses on expanding loading and unloading isolation and assembly, and MEF is focused on discoverability, scalability and portability. Both frameworks can interoperate smoothly, and a single application program can use these two frames at the same time.

In this paper a simple test MEF,
first create a new console project and add a reference to MEF framework, both System.ComponentModel.Composition.dll, and then type the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MEFDemo
{
    class Program
    {
        //导入对象使用
        [Import("chinese_hello")]
        public Person oPerson { set; get; }

        [Import("TestProperty")]
        public string ConsoleTest { get; set; }

        [Import("ActionTest")]
        public Action<string> TestActionImport { set; get; }

        [Import("FuncTest")]
        public Func<string> TestFuncImport { set; get; }

        [Import("FuncTestvalue")]
        public Func<string,string> TestFuncImport2 { set; get; }
        //多参数
       // public Func<string, string,string> TestFuncImport2 { set; get; }
        static void Main(string[] args)
        {
            var oProgram = new Program();
            oProgram.MyComposePart();
#if false
            //测试一
            var strRes = oProgram.oPerson.SayHello("李磊");
            Console.WriteLine(strRes);
           
#endif
#if true
            /// / Test Two 
            oProgram.TestActionImport ( " Jim " ); // No Return Value 
            var A = oProgram.TestFuncImport (); // return a value 
            var B = oProgram.TestFuncImport2 ( " can not pass argument " ); // may return a value parameter passing
             // Console.WriteLine (A); 
            Console.WriteLine (B);
 #endif 
            Console.Read (); 
        } 
        // host MEF member composition and 
        void MyComposePart () 
        { 
            // Assembly.GetExecutingAssembly () : The current method where the assembly
             //Assembly.GetCallingAssembly (): the current method of assembly calls a method where 
            var Catalog = new new AssemblyCatalog (Assembly.GetExecutingAssembly ());
             var Container = new new the CompositionContainer (Catalog);
             // add component (Part) to the host program and composition container 
            container.ComposeParts ( the this ); 
        } 
    } 
    public  class TestPropertyImport 
    { 
        [the export ( " TestProperty " )]
         public  String TestMmport { GET { return  " test can import and export property " ;}}

        [Export("ActionTest", typeof(Action<string>))]
        public void TestAction(string name)
        {
            Console.WriteLine("Hello:" + name);
        }

        [Export("FuncTest", typeof(Func<string>))]
        public string  TestFunc()
        {
            return "Hello:" + "1111";
        }
        [Export("FuncTestvalue", typeof(Func<string,string>))]
        public string TestFuncValue(string name)
        {
            return "Hello:" + name;
        }
    }
    //声明对象可以导出
    [Export("chinese_hello", typeof(Person))]
    public class Chinese : Person
    {
        public string SayHello(string name)
        {
            return "你好:" + name;
        }
    }
    [Export("american_hello", typeof(Person))]
    public class American : Person
    {
        public string SayHello(string name)
        {
            return "Hello:" + name;
        }
    }
    public interface Person
    {
        string SayHello(string name);
    }
}

 

Guess you like

Origin www.cnblogs.com/macT/p/11290535.html