繁体   English   中英

按基本类型注册类型,并在运行时使用Autofac解析

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM