获取错误未找到与约束匹配的有效导出 [英] Getting Error in No valid exports were found that match the constraint
问题描述
运行以下程序时,我得到
While running the following Program i am getting the
组合不变。由于以下错误,更改被拒绝:组合生成单个组合错误。根本原因如下。查看CompositionException.Errors属性以获取更详细的信息。
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)没有找到与约束匹配的有效导出((exportDefinition.ContractName ==System.Collections。 (ExportDefinition.Metadata.gettainItem(ExportTypeIdentity))和AndAlso(ExportDefinition.Metadata.ContainsKey(ExportTypeIdentity)AndAlsoSystem.Collections.Generic.IEnumerable(MEF.IToolWindow) ))),无效的导出可能已被拒绝。
1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "System.Collections.Generic.IEnumerable(MEF.IToolWindow)") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "System.Collections.Generic.IEnumerable(MEF.IToolWindow)".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
导致:无法设置import'MEF.Program + Application.ToolWindows(ContractName =System.Collections.Generic 。部分'MEF.Program + Application'上的.IEnumerable(MEF.IToolWindow))'。
元素:MEF.Program + Application.ToolWindows(ContractName =System.Collections.Generic.IEnumerable(MEF.IToolWindow)) - > MEF.Program + Application
Resulting in: Cannot set import 'MEF.Program+Application.ToolWindows (ContractName="System.Collections.Generic.IEnumerable(MEF.IToolWindow)")' on part 'MEF.Program+Application'. Element: MEF.Program+Application.ToolWindows (ContractName="System.Collections.Generic.IEnumerable(MEF.IToolWindow)") --> MEF.Program+Application
代码在此处
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel.Composition.Hosting;
using System.Collections;
//Without MEF
namespace MEF
{
public interface IToolWindow
{
string Name { get; set; }
}
public interface IMenuService
{
IMenu GetMenu(string menuName);
}
public interface IMenuItem
{
string Name { get; set; }
string Title { get; set; }
Action Handler { get; set; }
}
public interface IMenu
{
string Name { get; set; }
string Title { get; set; }
string ToolWindow { get; set; }
IEnumerable<IMenuItem> Items { get; set; }
IMenuItem GetItem(string itemName);
}
//public interface IMenuItem<> :IMenuItem{}
class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer();
var compositionBatch = new CompositionBatch();
var someToolWindow = new SomeToolWindow();
var application = new Application();
var menuService = new MenuService();
compositionBatch.AddPart(application);
compositionBatch.AddPart(someToolWindow);
compositionBatch.AddPart(menuService);
container.Compose(compositionBatch);
//application.ToolWindows = new List<IToolWindow>{someToolWindow};
//someToolWindow.MenuService = menuService;
foreach(var window in application.ToolWindows)
Console.WriteLine(window.Name);
Console.WriteLine(someToolWindow.MenuService);
Console.ReadLine();
}
public class Application
{
[Import]
public IEnumerable<IToolWindow> ToolWindows{get;set;}
public IToolWindow GetWindow(string windowName)
{
return ToolWindows.Where(w =>w.Name == windowName).FirstOrDefault();
}
}
[Export(typeof(IToolWindow))]
public class SomeToolWindow : IToolWindow
{
[Import]
public IMenuService MenuService { get; set; }
public string Name { get; set; }
public string Title { get; set; }
}
[Export(typeof(IMenuService))]
public class MenuService:IMenuService
{
public IEnumerable<IMenu> Menus {get;set;}
public IMenu GetMenu(string menuName)
{
return Menus.Where(m => m.Name == menuName).FirstOrDefault();
}
}
}
}
任何人都有想法?
推荐答案
[ImportMany]如果要填充列表。我测试了下面的代码并且工作。
You need to change the [Import] to [ImportMany] if you want to populate a list. I tested the code below and it works.
public class Application
{
[ImportMany]
public IEnumerable<IToolWindow> ToolWindows { get; set; }
public IToolWindow GetWindow(string windowName)
{
return ToolWindows.Where(w => w.Name == windowName).FirstOrDefault();
}
}
此外,您还需要为SomeToolWindow添加一个名称if你想要它打印它。
Also you will need to add a name to SomeToolWindow if you want it to print it.
这篇关于获取错误未找到与约束匹配的有效导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!