2010-12-06 66 views
5

我有一個關於下列例外,我在試圖完成一個呼叫.ComposeParts(this)問題:試圖找出這是什麼MEF組成錯誤意味着

的組合物生產的單 組成錯誤。根本原因是 下面提供。查看 CompositionException.Errors屬性 以獲取更多詳細信息。

1)出口 'CustomersModule.CustomerMenu (ContractName = 「ModLibrary.IMenu」)' 是 不能分配給輸入 「System.Collections.Generic.IEnumerable`1 [[ModLibrary.IMenu, ModLibrary,版本= 1.0.0.0, Culture = neutral, PublicKeyToken = null]]'。

,導致:無法設置進口 上 部分 'ModAppWorks.Host' ModAppWorks.Host.Menus (ContractName = 「ModLibrary.IMenu」)「。元素: 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需要,沒有人看到的東西我可能是做錯了?

回答

6

當您導入一個導出集合時,您需要使用ImportMany屬性來明確它。更改屬性特性是這樣的:

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

你也應該能夠排除合同(「typeof運算(菜單)」參數),因爲你要導入已導出同一類型。儘管保留導出屬性上的合同。

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

bah..dang 5分鐘計時器。如果您幾分鐘內沒有看到答案,請回複評論。 – IAbstract 2010-12-06 03:57:32