2011-02-17 203 views
1

我從網上得到一個簡單的示例,使用System.ComponentModel.Composition.dll版本工作在.NET 3.5框架精細V2.0.50727MEF - 遷移從.NET 3.5到.NET 4.0

我已經更改了項目定義並將目標更改爲.NET 4.0,並且它工作得很完美。

當我替換上面的.dll文件的V2.0.50727版本是v4.0.30319我得到的容器的組成過程中抱怨錯誤的最新版本。它打破了代碼如下:


     private void LoadPlugins() { 
      var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); 

      var container = new CompositionContainer(catalog); 
         container.ExportsChanging += new EventHandler(container_ExportsChanging); 

      var batch = new CompositionBatch(); 
      batch.AddPart(this); 
      container.Compose(batch); // throws Exception 
     } 

而例外的是以下內容:


System.ComponentModel.Composition.ChangeRejectedException was unhandled 
Message=The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. 

1) More than one export was found that matches the constraint '((exportDefinition.ContractName == "MefTutorial.IPlugin") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "MefTutorial.IPlugin".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'. 

Resulting in: Cannot set import 'MefTutorial.PluginConsumer._myPlugins (ContractName="MefTutorial.IPlugin")' on part 'MefTutorial.PluginConsumer'. 
Element: MefTutorial.PluginConsumer._myPlugins (ContractName="MefTutorial.IPlugin") --> MefTutorial.PluginConsumer 

什麼我需要做的遷移到.NET 4.0關於MEF

回答

0

好的,我發現了這個問題。顯然,在之前版本的符號是在我以前的評論中提到,但在新的.NET 4.0版本導入的語法應爲:

code> 
[ImportMany(typeof(IPlugin))] 
internal List _myPlugins { get; set; } 

注意使用列表ImportMany的代替IList和導入。

1

難道是另一個項目仍然引用.net 3.5版本嗎?該錯誤消息說有兩個輸出類型IPlugin,我很確定的意思是可以找到dll的3.5和4.0版本。

檢查只有MefTutorial的4.0版本被引用和/或存在。

+0

我會繼續這個。 Visual Studio的「清潔」是垃圾。它讓我有一天在追逐類似的錯誤。只是當我檢查輸出文件夾時,我意識到它仍然充滿了舊文件... – Tim 2011-02-17 11:48:16

+0

是的,我也遇到了這個問題,還有MEF。它仍然發現老dll並抱怨。 – Femaref 2011-02-17 12:54:57