【发布时间】: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 所需要的,有人看到我可能做错了什么吗?
【问题讨论】: