我定义了以下接口:
public interface ICustomService<T> where T : CustomObject
{
IEnumerable<T> GetById(int Id);
...
}
和2个it实现,其中MyObject1
&;MyObject2
都是从CustomObject
继承来的
public class CustomService1 : ICustomService<MyObject1>
{
public IEnumerable<MyObject1> GetById(int Id)
{
...
}
}
public class CustomService2 : ICustomService<MyObject2>
{
public IEnumerable<MyObject2> GetById(int Id)
{
...
}
}
我try 将这两个都注册为ICustomService<CustomObject>
,但出现了错误:
没有从"CustomerService 1"到"ICCustomService"的隐式引用转换<;CustomObject>;'
而是这样注册:
services.AddTransient<ICustomService<MyObject1>, CustomService1>();
services.AddTransient<ICustomService<MyObject2>, CustomService2>();
当像上面那样注册时,我的IEnumerable services
是空的:
public ThirdService(IEnumerable<ICustomService<CustomObject>> services)
{
}
如何将ICustomService
的所有实现注入ThirdService
?
我正在try 这样做,以便ThirdService
可以得到一个ID,然后在所有服务上使用GetById
获取所有CustomObject
个ID.