MEF宿主應用程式

來源:互聯網
上載者:User

在應用程式中宿主MEF

 

Managed Extensibility Framework(MEF)是.NET平台下的一個擴充性管理架構,他的特點如下:

  1、.net下的一個可擴充行管理架構,包括了依賴注入和Duck Typing。
  2、 輕鬆應對應用程式擴充並且對已有代碼產生最小影響。
  3、 應用程式和擴充程式之間不產生直接依賴,多個同樣需求共用擴充程式。

 

參照http://mef.codeplex.com上面的教程嘗試實現了一個樣本

實現的步驟如下:
1、建立一個wpf的Console Application,宿主就是Program class
2、添加System.ComponentModel.Composition程式集和相關引用
3、添加一個Compone()方法,建立組合容器
4、添加一個方法,調用Compone()方法
5、在Main方法裡執行個體化宿主
6、定義在宿主中匯出的組件
7、在宿主中匯出組件
8、添加組件到容器,方法有二
   1.直接添加組合組件執行個體
   2.使用目錄添加組合組件

 

實現代碼:

using System;using System.ComponentModel.Composition;using System.ComponentModel.Composition.Hosting;namespace MEFConsole{    /// <summary>    /// 宿主    /// </summary>    class Program    {        static void Main(string[] args)        {            Program p = new Program();            p.Run1();            p.Run2();        }        /// <summary>        /// 匯入        /// </summary>        [Import]        public IWaiter waiter { get; set; }        /// <summary>        /// 執行個體化組合容器        /// 直接添加組件        /// </summary>        private void Compone1()        {            var container = new CompositionContainer();            container.ComposeParts(this, new Waiter());        }        /// <summary>        /// 測試直接添加組件方法        /// </summary>        public void Run1()        {            Console.WriteLine("直接添加組件到容器方式:");            Compone1();            waiter.Welcome();            waiter.Serve("糖醋裡脊");            waiter.GoodBye();            Console.WriteLine();            //Console.Read();        }        /// <summary>        /// 執行個體化組合容器類        /// 通過目錄添加組件        /// </summary>        private void Compone2()        {            var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());            var container2 = new CompositionContainer(catalog);            container2.ComposeParts(this);        }        /// <summary>        /// 測試通過目錄添加控制項        /// </summary>        public void Run2()        {            Console.WriteLine("通過目錄添加組件到容器方式:");            Compone2();            waiter.Welcome();            waiter.Serve("糖醋裡脊");            waiter.GoodBye();            Console.Read();        }    }    /// <summary>    /// 聲明服務員介面    /// </summary>    public interface IWaiter    {        void Welcome();        void Serve(string name);        void GoodBye();    }    /// <summary>    /// 服務員類    /// 標記為匯出    /// </summary>    [Export(typeof(IWaiter))]    public class Waiter : IWaiter    {        public void Welcome()        {            Console.WriteLine("歡迎光臨!");        }        public void Serve(string name)        {            Console.WriteLine("先生您點的菜" + name + "來了,請慢用!");        }        public void GoodBye()        {            Console.WriteLine("歡迎下次光臨!");        }    }}

執行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.