我正在尝试在我的应用中使用MEF2无属性/基于约定的注册。我所看到的是,如果省略[Export]属性, ComposeParts 调用或 GetExports 调用都不会导致任何项目。
如果我将 [Export(typeof(IGuiModule))] 属性添加到我的班级,那么它会被提起,但是会有(合理的)警告:
"An Export specification convention that would apply to type 'Core.Models.DeviceListView' has been overridden by attributes applied in the source file or by a prior convention."
我在这里错过了什么,或者有错误的期望吗?我被引导相信MEF2方法允许您导入而不需要显式的导出属性?
我发现的信息有点混乱和碎片化,各种来源显示不同的信息(正如MEF已经发展,我想)。
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class)]
public class ModuleMetadata : Attribute, IModuleMetadata
{
public ModuleMetadata(string aName)
{
Name = aName;
}
public string Name { get; private set; }
}
public class Core
{
[ImportMany(typeof(IGuiModule))]
public List<ExportFactory<IGuiModule, IModuleMetadata>> GuiModuleImports;
public Core()
{
var rb = new RegistrationBuilder();
rb.ForTypesDerivedFrom<IGuiModule>().Export<IGuiModule>();
var catalog = new DirectoryCatalog(@"d:\prog\core\", "*.dll", rb);
var container = new CompositionContainer(catalog);
var gx = container.GetExports<IGuiModule, IModuleMetadata>();
container.ComposeParts(this);
}
}
导出类:
// We appear to *need* this attribute: [Export(typeof(IGuiModule))]
[ModuleMetadata("Device List")]
public partial class DeviceListView : UserControl, IGuiModule
{
public DeviceListView() {...}
}