开发者

Trying to figure out what this MEF Composition error means

I have a question about the following exception I received trying to complete a call to .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

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
开发者_JAVA技巧    [Import(typeof(IMenu))]
    public IEnumerable<IMenu> Menus { get; set; }
    #endregion

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

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


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; } 

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; } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜