MEF初步使用

            MEF(Managed Extensibility Framework)是一個用於建立可擴展的輕型應用程序的庫。 應用程序開發人員可利用該庫發現並使用擴展,而無需進行配置。 擴展開發人員還能夠利用該庫輕鬆地封裝代碼,避免生成脆弱的硬依賴項。 經過 MEF,不只能夠在應用程序內重用擴展,還能夠在應用程序之間重用擴展。(摘自MSDN)c#

            我初步使用後的感覺是經過這個應用程序庫,咱們能夠實現接口功能的擴展和自動導入到實例,不須要本身編碼對接口進行實現。它能夠在指定的程序集中搜索匹配合適的接口實現,而後導入使用。app

弄了一個控制檯程序,代碼很少,很簡單,我就直接上代碼了。看完了應該會有一個初步認知。編碼

文件一:program.csspa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MEFdemo
{
    class Program
    {

        public IbookService book { get; set; }
        public IFoodService Food { get; set; }
        public CompositionContainer container { get; set; }
        static void Main(string[] args)
        {
            Program pro = new Program();
            pro.Compose();
            pro.book = pro.container.GetExportedValueOrDefault<IbookService>("musicbook");
            Console.WriteLine(pro.book.GetBookName());
            pro.book = pro.container.GetExportedValueOrDefault<IbookService>("historybook");
            Console.WriteLine(pro.book.GetBookName());
            pro.Food = pro.container.GetExportedValueOrDefault<IFoodService>("musicbook");
            if (pro.Food == null)
                pro.Food = pro.container.GetExportedValue<IFoodService>();//GetExportedValue與GetExportedValueOrDefault的區別
                                                                          // 在於後者找不到匹配度的,會異常報錯
            Console.WriteLine(pro.Food.GetFoodName());
            Console.WriteLine("當前程序集路徑:" + Assembly.GetExecutingAssembly().Location);
            Console.Read();
        }
        /// <summary>
        /// 構造好組合容器
        /// </summary>
        private void Compose()
        {
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());//指定程序集目錄
            container = new CompositionContainer(catalog);//設定部件容器爲該目錄下
        }

    }
}

文件二:initalInterFace.cscode

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MEFdemo
{
    public interface IbookService
    {
        string BookName { get; set; }
        string GetBookName();
    }
    public interface IFoodService
    {
        string FoodName { get; set; }
        string GetFoodName();
    }
    [Export("musicbook", typeof(IbookService))]
    public class MusicBook : IbookService
    {
        public string BookName { get; set; }
        public string GetBookName()
        {
            return "MusicBook";
        }
    }
    [Export("historybook", typeof(IbookService))]
    public class HistoryBook : IbookService
    {
        public string BookName { get; set; }
        public string GetBookName()
        {
            return "HistoryBook";
        }
    }
    [Export("musicbook", typeof(IFoodService))]
    public class apple:IFoodService{
        public string FoodName { get; set; }
        public string GetFoodName()
        {
            return "蘋果";
        }
    }
}

其實上面的代碼,展現瞭如何使用Export導出實現後的接口,已經取相同名字不一樣接口,不一樣名字相同接口的實現狀況。接口

有興趣的能夠耐心看看。開發

運行結果以下:get

相關文章
相關標籤/搜索