gpt4 book ai didi

c# - ImportMany 不适用于自定义元数据属性

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:08 25 4
gpt4 key购买 nike

我在 MEF 中为 ImportMany 创建了自定义元数据属性,但 ImportMany 始终为空。

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]
public class ModuleAttribute : ExportAttribute ,IModuleMetadata
{
public ModuleAttribute(string contract)
: base(typeof(IScreen))
{
Name = contract;
Region = Region.Sidebar;
IsVisible = true;
}

public string Name { get; set; }
public string Module { get; set; }
public Region Region { get; set; }
public string DisplayName { get; set; }
public bool IsVisible { get; set; }
public string Description { get; set; }
public short Order { get; set; }
public Permissions Permissions { get; set; }
}

下面是我导出类的方式。

[Module("Stock" ,Module = "Stock" ,Order = 1 ,Permissions = Permissions.View)]
internal sealed class StockWorkSpaceViewModel : Screen

其中 Screen 是 typeof(IScreen)

这是我的 ImportMany。

[ImportMany]
public IEnumerable<Lazy<IScreen ,IModuleMetadata>> Modules { get; set; }

任何人都可以预测我哪里出错了。

最佳答案

  1. 检查 ComponnetContainer.ComposeParts
  2. 检查导出和导入属性必须相同
  3. 检查您在惰性初始化中使用的自定义属性和接口(interface)。属性必须相同。这意味着海关属性类实现了延迟初始化隐式。

关于c# - ImportMany 不适用于自定义元数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507171/

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