试图找出此MEF组成错误的含义 [英] Trying to figure out what this MEF Composition error means

查看:101
本文介绍了试图找出此MEF组成错误的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下尝试完成对.ComposeParts(this)的呼叫所收到的异常有疑问:

I have a question about the following exception I received trying to complete a call to .ComposeParts(this):

该组合产生了一个 组成错误.根本原因是 在下面提供.查看 CompositionException.Errors属性 有关更多详细信息.

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1)出口 'CustomersModule.CustomerMenu (ContractName ="ModLibrary.IMenu")'是 不可分配给类型 'System.Collections.Generic.IEnumerable`1 [[ModLibrary.IMenu, ModLibrary,版本= 1.0.0.0, 文化=中立, PublicKeyToken = null]]".

1) The export 'CustomersModule.CustomerMenu (ContractName="ModLibrary.IMenu")' is not assignable to type 'System.Collections.Generic.IEnumerable`1[[ModLibrary.IMenu, ModLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.

结果:无法设置导入 'ModAppWorks.Host.Menus (ContractName ="ModLibrary.IMenu")' 部分"ModAppWorks.Host".元素: ModAppWorks.Host.Menus (ContractName ="ModLibrary.IMenu")-> ModAppWorks.Host

Resulting in: Cannot set import 'ModAppWorks.Host.Menus (ContractName="ModLibrary.IMenu")' on part 'ModAppWorks.Host'. Element: ModAppWorks.Host.Menus (ContractName="ModLibrary.IMenu") --> ModAppWorks.Host

那里有一个部分看起来似乎错误意味着IMenu必须实现IEnumerable.这是我的撰写代码:

There is a part there that seems like the error means that IMenu must implement IEnumerable. Here is my composition code:

static class Program
{
    [STAThread]
    static void Main()
    {
        Host host = new Host();
        host.Run();
    }
}

class Host
{
    #region Init
    public Host()
    { }
    #endregion

    #region Functions
    public void Run()
    {
        Compose();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new AppHost());
    }

    private void Compose()
    {
        var agrCatalog = new AggregateCatalog();
        var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName
            (Assembly.GetExecutingAssembly().Location) + "..\\..\\..\\Extensions", "*.dll");
        var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

        agrCatalog.Catalogs.Add(dirCatalog);
        agrCatalog.Catalogs.Add(asmCatalog);

        var hostContainer = new CompositionContainer(agrCatalog);
        hostContainer.ComposeParts(this);
    }
    #endregion

    #region Properties
    [Import(typeof(IMenu))]
    public IEnumerable<IMenu> Menus { get; set; }
    #endregion

我正在导入实例ToolStripMenuItem的类.我的出口样品:

I am importing a class that instances a ToolStripMenuItem. My export sample:

[Export(typeof(IMenu))]
public class CustomerMenu : IMenu
{
    #region Fields
    private System.Windows.Forms.ToolStripMenuItem CustomerMainMenu;
    private System.Windows.Forms.ToolStripSeparator mnuSeparator;
    private System.Windows.Forms.ToolStripMenuItem CustomersMenuItem;
    #endregion

    #region Init
    public CustomerMenu()
    {
        InitializeComponent();
        // 
        // CustomerMenu
        // 
        this.CustomerMainMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.mnuSeparator,
        this.CustomersMenuItem});
        this.CustomerMainMenu.Name = "CustomerMenu";
        this.CustomerMainMenu.Size = new System.Drawing.Size(94, 20);
        this.CustomerMainMenu.Text = "Customer Menu";
        // 
        // toolStripMenuItem1
        // 
        this.mnuSeparator.Name = "toolStripMenuItem1";
        this.mnuSeparator.Size = new System.Drawing.Size(149, 6);
        // 
        // Customers
        // 
        this.CustomersMenuItem.Name = "Customers";
        this.CustomersMenuItem.Size = new System.Drawing.Size(152, 22);
        this.CustomersMenuItem.Text = "Customers";
    }

    #endregion

    #region Functions
    private void InitializeComponent()
    {
        this.CustomerMainMenu = new System.Windows.Forms.ToolStripMenuItem();
        this.mnuSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.CustomersMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    }

    #endregion

如果IMenu不是实现IEnumerable所必需的 ,那么有人看到我可能做错了什么吗?

If IMenu is not required to implement IEnumerable, does anyone see something I might be doing wrong?

推荐答案

在导入导出集合时,需要使用ImportMany属性对其进行明确说明.像这样更改您的属性属性:

When you're importing a collection of exports, you need to be explicit about it by using the ImportMany attribute. Change your property attribute like this:

[ImportMany(typeof(IMenu))] 
public IEnumerable<IMenu> Menus { get; set; } 

您还应该能够排除合同("typeof(Menu)"参数),因为您要导入与导出的类型相同的合同.不过,将合同保留在导出"属性上.

You should also be able to exclude the contract (the "typeof(Menu)" parameter) since you're importing the same type that was exported. Leave the contract on the Export attributes though.

[ImportMany] 
public IEnumerable<IMenu> Menus { get; set; } 

这篇关于试图找出此MEF组成错误的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆