也來學學外掛程式式開發續-利用MEFMEF程式設計指南一:在應用程式中宿主MEF【.Net平台下外掛程式開發】-MEF與MAF初步調研《MEF程式設計指南》博文匯總

來源:互聯網
上載者:User

前面一個部落格:也來學學外掛程式式開發中很多朋友留言說可以用MEF來實現。於是我就試著用MEF實現了一下。

步驟和上一篇差不多,只是載入外掛程式的方式有所不同。這隻是一個自己的樣本程式,肯定有很多不足之處,歡迎拍磚。

MEF如何工作?

MEF主要是通過Import與Export特性來定義匯入與匯出組件。程式在啟動並執行時候會將具有相同介面的匯出的執行個體化,賦給匯入。

MEF程式設計指南一:在應用程式中宿主MEF這篇文章有一個簡單的MEF例子,大家可以參考一下。

所以,在這裡,外掛程式就是匯出,我們在程式中要定義具有和外掛程式相同介面的匯入。

但是這裡有一個問題,一般的匯入與匯出是一對一的,但我們的工具箱中和外掛程式的關係明顯是一對多的怎麼辦?

沒問題,MEF對於這種情況可以將匯入聲明為ImportMany,這樣就支援一個匯入對應多個匯出了。

關於ImportMany可參見:http://msdn.microsoft.com/en-us/library/dd460648.aspx#further_imports_and_importmany

因此這裡,我用ImportMany來定義一個集合來儲存外掛程式:

        [ImportMany]        public IEnumerable<Iplugin> plugins;
如何讓MEF發現外掛程式?

我們用反射的時候是將外掛程式放置在一個固定的目錄中,然後再去掃描這個目錄來發現外掛程式,在MEF中如何來發現外掛程式呢?

MEF提供三種方式發現組件:

  1. AssemblyCatalog 在當前程式集發現組件。
  2. DirectoryCatalog 在指定的目錄發現組件。
  3. DeploymentCatalog 在指定的XAP檔案中發現組件(用於silverlight)

可以看到,我們也可以將外掛程式放在統一的目錄讓MEF去檢索發現。

使用MEF

使用MEF的時候,首先要初始化MEF的組合容器物件:CompositionContainer,所以在表單載入的時候要做好初始化工作。

        public ToolBox()        {            InitializeComponent();            Init();        }        private CompositionContainer _container;        private void Init()        {            //An aggregate catalog that combines multiple catalogs            var catalog = new AggregateCatalog();            //設定目錄            catalog.Catalogs.Add(new DirectoryCatalog(Application.StartupPath + "\\plugin\\"));            //Create the CompositionContainer with the parts in the catalog            _container = new CompositionContainer(catalog);            //Fill the imports of this object            try            {                this._container.ComposeParts(this);            }            catch (CompositionException compositionException)            {                Console.WriteLine(compositionException.ToString());            }        }

接著就是發現外掛程式後的顯示工作了:

        private void ToolBox_Load(object sender, EventArgs e)        {            InitPlugin();        }        [ImportMany]        public IEnumerable<Iplugin> plugins;        public void InitPlugin()        {            foreach (Iplugin plugin in plugins)            {                InitModule(plugin);            }        }
增加外掛程式

我們來新增一個外掛程式試試。建立一個類庫項目,再增加一個Window表單,拉一個PictureBox,顯示一張圖片。主要的工作是我們要定義匯出:Export

   [Export(typeof(PluginMain.Interface.Iplugin))]    public class App:PluginMain.Interface.Iplugin    {        public System.Windows.Forms.Form MainForm        {            get { return new Picture(); }        }        public System.Drawing.Image ModulePicture        {            get { return null; }        }    }

將產生的.DLL放在plugin目錄中。產生後效果

 範例程式碼下載:點我

 

參考資料:【.Net平台下外掛程式開發】-MEF與MAF初步調研

             《MEF程式設計指南》博文匯總

             Managed Extensibility Framework (MEF)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.