2013-02-08 36 views
0

好吧,在類庫裏面,不使用MEF是個好主意嗎?在類庫中使用MEF

下面是一個例子:

ISomeInterface

ISomeInterface

5個實現

一旦類進口所有ISomeInterface並使用它們。

再次,這是所有在一個單一的DLL。由於它在DLL中,所以沒有引導MEF來創建目錄,並且爲了僅使用一次目錄而構建目錄似乎有點多。

剛剛學習MEF以及如何使用它。

格雷格

+0

什麼你的問題? – 2013-02-08 21:42:41

+0

那麼,我是否應該不像描述的那樣在DLL中使用MEF? IE,這不是一個好主意嗎? – 2013-02-08 23:06:59

回答

1

讀了一點就在這之後,它看起來像有任何理由,MEF不能爲它自己的零件製造一個DLL內部使用。當我問這個問題時,我一直認爲Importing主要是在Main()或App()類型的函數中構成整個應用程序。但是,如果構成需要對被出口到應用程序的重要組成部分來完成,它仍然可以使用MEF組合本身在構造函數中,像這樣:

//An aggregate catalog that combines multiple catalogs 
     var catalog = new AggregateCatalog(); 
     //Adds all the parts found in the same assembly as the Program class 
     catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); 

     //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) 
     { 

     }