【发布时间】:2021-11-04 08:03:34
【问题描述】:
我正在尝试使用 [Export] 属性和注册生成器填充 MEF 目录。 但是使用属性导出的服务可以解析,使用RegistrationBuilder注册的服务无法解析。 看看我下面的代码:
static class Program
{
[STAThread]
static void Main()
{
// export service 2 using registration builder
var builder = new RegistrationBuilder();
builder.ForType<IService2>().Export<Service2>();
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder);
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
container.SatisfyImportsOnce(builder);
// service exported using attribute can be resolved
var service1 = container.GetExport<IService1>();
// throws
var service2 = container.GetExport<IService2>();
}
}
// create two dummy services
public interface IService1 { }
public interface IService2 { }
// export service 1 with attribute
[Export(typeof(IService1))]
public class Service1 : IService1 { }
public class Service2 : IService2 { }
我做错了什么?有人可以帮助我吗?谢谢!
【问题讨论】: