c# - 试图找出此 MEF 组合错误的含义

标签 c# winforms mef

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

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

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]]'.

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。这是我的组合代码:

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

最佳答案

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

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

您还应该能够排除契约(Contract)(“typeof(Menu)”参数),因为您导入的类型与导出的类型相同。不过,保留关于导出属性的约定。

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

关于c# - 试图找出此 MEF 组合错误的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363022/

相关文章:

c# - 如何在C#中调整按钮上的图片大小?

.net - 使用 ComboBox 的自动完成功能,同时将值限制为列表中的值?

c# - WinForm 应用程序中的 Microsoft 报表教程

c# - nvelocity最新源码在哪里

vb.net - 将未处理的异常记录到日志文件

c# - Ninject 相当于 MEF AssemblyCatalog

c# - 具有多个构造函数的 MEF 构造函数参数

c# - .NET 中性能至上且数据形式不重要的场景的最佳序列化?

C# XML 后代查询

wpf - Caliburn micro wpf 多个项目