【问题标题】:Getting Error in No valid exports were found that match the constraint在没有找到与约束匹配的有效导出时出错
【发布时间】:2013-08-01 03:04:03
【问题描述】:

在运行以下程序时,我得到了

构图保持不变。由于以下错误,更改被拒绝: 合成产生了单个合成错误。下面提供了根本原因。查看 CompositionException.Errors 属性以获取更多详细信息。

1) 找不到与约束匹配的有效导出 '((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"))))',无效的导出可能已被拒绝。

导致:无法在部件“MEF.Program+Application”上设置导入“MEF.Program+Application.ToolWindows (ContractName="System.Collections.Generic.IEnumerable(MEF.IToolWindow)”)'。 元素: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();
        }
    }
}

}

有人有想法吗?

【问题讨论】:

    标签: .net c#-4.0 mef


    【解决方案1】:

    如果要填充列表,则需要将 [Import] 更改为 [ImportMany]。我测试了下面的代码,它可以工作。

       public class Application
       {
           [ImportMany]
           public IEnumerable<IToolWindow> ToolWindows { get; set; }
    
           public IToolWindow GetWindow(string windowName)
           {
               return ToolWindows.Where(w => w.Name == windowName).FirstOrDefault();
           }
       }
    

    如果你想打印它,你还需要给 SomeToolWindow 添加一个名字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2022-10-06
      • 1970-01-01
      • 2014-02-17
      • 2014-07-13
      • 2013-10-13
      • 2016-08-03
      • 2013-07-09
      相关资源
      最近更新 更多