【问题标题】:Trying to figure out what this MEF Composition error means试图弄清楚这个 MEF 组合错误意味着什么
【发布时间】:2010-12-06 03:46:46
【问题描述】:

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

该组合产生了一个单一的 组成错误。根本原因是 下面提供。审查 CompositionException.Errors 属性 了解更多详细信息。

1) 出口 '客户模块.客户菜单 (ContractName="ModLibrary.IMenu")' 是 不可分配给类型 'System.Collections.Generic.IEnumerable`1[[ModLibrary.IMenu, ModLibrary,版本=1.0.0.0, 文化=中性, PublicKeyToken=null]]'。

导致:无法设置导入 'ModAppWorks.Host.Menus (ContractName="ModLibrary.IMenu")' 开启 部分“ModAppWorks.Host”。元素: ModAppWorks.Host.Menus (ContractName="ModLibrary.IMenu") --> ModAppWorks.Host

那里有一部分似乎错误意味着IMenu 必须实现IEnumerable。这是我的作文代码:

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 的类。我的出口样本:

[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 所需要的,有人看到我可能做错了什么吗?

【问题讨论】:

    标签: c# winforms mef


    【解决方案1】:

    当您导入一个导出集合时,您需要使用 ImportMany 属性明确说明它。像这样更改您的属性:

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

    您还应该能够排除合同(“typeof(Menu)”参数),因为您导入的类型与导出的类型相同。将合同保留在 Export 属性上。

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

    【讨论】:

    • 呸..当5分钟计时器。如果您在几分钟内没有看到接受的答案...请回复。
    • 我终于掌握了使用 MEF 的窍门。到目前为止,我是一个粉丝。
    • @dboarman 是的,当它是 CTP 形式时,我玩了很多次,现在仍然涉足。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多