我有一个问题,我收到的以下异常试图完成一个电话.ComposeParts(this):
该组合物产生单一组成误差.根本原因如下.查看CompositionException.Errors属性以获取更多详细信息.
1)导出'CustomersModule.CustomerMenu(ContractName ="ModLibrary.IMenu")'不能分配给'System.Collections.Generic.IEnumerable`1 [[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
Run Code Online (Sandbox Code Playgroud)
我正在导入一个实例a的类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
Run Code Online (Sandbox Code Playgroud)
如果IMenu是未实现所需IEnumerable,没有人看到的东西我可能是做错了?
导入导出集合时,需要使用ImportMany属性对其进行明确说明.像这样更改属性属性:
[ImportMany(typeof(IMenu))]
public IEnumerable<IMenu> Menus { get; set; }
Run Code Online (Sandbox Code Playgroud)
您还应该能够排除合同("typeof(Menu)"参数),因为您导入的是导出的相同类型.保留合同的导出属性.
[ImportMany]
public IEnumerable<IMenu> Menus { get; set; }
Run Code Online (Sandbox Code Playgroud)