C# ------ MEF

参考

参考

MEF全称Managed Extensibility Framework, 是一个用于创建可扩展的轻型应用程序的库。开发人员可以利用MEF发现并使用扩展,但并不需要配置,而且还可以在应用程序之间重用扩展。应用程序的扩展是一个大型应用程序架构师必须考虑的一个问题。以前的解决办法通常是用配置文件,接口及反射技术来进行实现。而MEF则 提供一种通过隐式发现组件的方法。MEF 组件以声明方式同时指定其依赖项(称为“导入”)及其提供的功能(称为“导出”)。MEF 是 .NET Framework 4 的组成部分,早期的 .NET Framework 版本引入了 Managed Add-in Framework (MAF),旨在使应用程序能够隔离和管理扩展。 MAF 的重点放在比 MEF 稍高的级别,它集中于扩展隔离以及程序集的加载和卸载,而 MEF 则集中于可发现性、扩展性和可移植性。 这两个框架可以顺利地进行互操作,并且单个应用程序可以同时利用这两个框架。

本文简单的测试一下MEF,
首先新建一个控制台工程.并添加MEF框架的引用,既 System.ComponentModel.Composition.dll,然后键入如下代码:

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
            ////测试二
            oProgram.TestActionImport("Jim");//无返回值
            var a = oProgram.TestFuncImport();//有返回值
            var b = oProgram.TestFuncImport2("可以传参");//可以传参有返回值
            //Console.WriteLine(a);
            Console.WriteLine(b);
#endif
            Console.Read();
        }
        //宿主MEF并组合部件
        void MyComposePart()
        {
            //Assembly.GetExecutingAssembly():当前方法所在程序集
            //Assembly.GetCallingAssembly():调用当前方法的方法 所在的程序集
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(catalog);
            //将部件(part)和宿主程序添加到组合容器
            container.ComposeParts(this);
        }
    }
    public class TestPropertyImport
    {
        [Export("TestProperty")]
        public string TestMmport { get { return "测试属性可以导入导出"; } }

        [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);
    }
}

猜你喜欢

转载自www.cnblogs.com/macT/p/11290535.html
今日推荐