gpt4 book ai didi

c# - 试图找出此 MEF 组合错误的含义

转载 作者:太空狗 更新时间:2023-10-29 23:11:02 25 4
gpt4 key购买 nike

我对在尝试完成对 .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

那里有一部分似乎错误意味着 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 所必需的,有没有人看到我可能做错了什么?

最佳答案

当您导入导出的集合时,您需要使用 ImportMany 属性对其进行明确说明。像这样更改您的属性:

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

您还应该能够排除契约(Contract)(“typeof(Menu)”参数),因为您导入的类型与导出的类型相同。不过,保留关于导出属性的约定。

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

关于c# - 试图找出此 MEF 组合错误的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com