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