2011-02-17 207 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