对于试图完成对.ComposeParts(this)的调用的以下异常,我有一个问题:
合成产生一个单一的合成错误。根本原因如下。查看CompositionException.Errors属性以获得更详细的信息。
1)输出'CustomersModule.CustomerMenu (ContractName=‘ModLibrary.IMenu’)不能指定为ModLibrary、Version=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,是否有人看到我可能做错了什么?
发布于 2010-12-06 03:51:34
在导入导出集合时,需要使用ImportMany属性对其进行显式说明。更改属性属性如下:
[ImportMany(typeof(IMenu))]
public IEnumerable<IMenu> Menus { get; set; } 您还应该能够排除契约(“type (菜单)”参数),因为您导入的是导出的相同类型。不过,将合同保留在Export上。
[ImportMany]
public IEnumerable<IMenu> Menus { get; set; } https://stackoverflow.com/questions/4363022
复制相似问题