[英]Register types by base type and resolve at runtime with Autofac
我有以下设置:
public interface IServiceBase {}
public interface IService1: IServiceBase {}
public interface IService2: IServiceBase {}
// ... Snip lots of lines
public interface IService999: IServiceBase {}
public interface IConnection { T GetService<T>() where T: IServiceBase; }
IConnection
已在Autofac中注册。 在其上调用GetService()
将动态实现给定的接口并返回一个实例。
现在,我想对所有IServiceBase
派生的类型进行某种通用注册。 我不想手动注册每个IServiceX
,也不想进行程序集扫描(尽管我认为这是一个选择)。
在不是真正合法的代码中,这就是我想要的:
ContainerBuilder.Register<IServiceBase>(x=> x.Resolve<IConnection>().GetService);
这可能吗?
看起来我需要IRegistrationSource
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.