C# 无法从系统转换。懒惰<;IARepository>;到系统。懒惰<;IGenericRepository>;
我试图用SimpleInjector实现延迟初始化,并使用泛型,但在基本构造函数中发送延迟时,我的AService.cs中出现了一个错误。我已经读到Lazy不支持协方差,但我的问题是如何在IoC和Lazy中使用泛型 AService.csC# 无法从系统转换。懒惰<;IARepository>;到系统。懒惰<;IGenericRepository>;,c#,generics,inversion-of-control,repository-pattern,lazy-initialization,C#,Generics,Inversion Of Control,Repository Pattern,Lazy Initialization,我试图用SimpleInjector实现延迟初始化,并使用泛型,但在基本构造函数中发送延迟时,我的AService.cs中出现了一个错误。我已经读到Lazy不支持协方差,但我的问题是如何在IoC和Lazy中使用泛型 AService.cs public class AService : GenericService, IAService { private readonly Lazy<IARepository> aRepository; public AService
public class AService : GenericService, IAService
{
private readonly Lazy<IARepository> aRepository;
public AService(Lazy<IARepository> aRepository) : base(aRepository)
{
this.aRepository = aRepository;
}
}
public interface IAService : IGenericService
{
}
public interface IGenericService
{
void DoSomething();
}
public class GenericService : IGenericService
{
private readonly Lazy<IGenericRepository> genericRepository;
public GenericService(Lazy<IGenericRepository> genericRepository)
{
this.genericRepository = genericRepository;
}
public void DoSomething()
{
genericRepository.Value.DoSomething();
}
}
IGenericService.cs
public class AService : GenericService, IAService
{
private readonly Lazy<IARepository> aRepository;
public AService(Lazy<IARepository> aRepository) : base(aRepository)
{
this.aRepository = aRepository;
}
}
public interface IAService : IGenericService
{
}
public interface IGenericService
{
void DoSomething();
}
public class GenericService : IGenericService
{
private readonly Lazy<IGenericRepository> genericRepository;
public GenericService(Lazy<IGenericRepository> genericRepository)
{
this.genericRepository = genericRepository;
}
public void DoSomething()
{
genericRepository.Value.DoSomething();
}
}
GenericService.cs
public class AService : GenericService, IAService
{
private readonly Lazy<IARepository> aRepository;
public AService(Lazy<IARepository> aRepository) : base(aRepository)
{
this.aRepository = aRepository;
}
}
public interface IAService : IGenericService
{
}
public interface IGenericService
{
void DoSomething();
}
public class GenericService : IGenericService
{
private readonly Lazy<IGenericRepository> genericRepository;
public GenericService(Lazy<IGenericRepository> genericRepository)
{
this.genericRepository = genericRepository;
}
public void DoSomething()
{
genericRepository.Value.DoSomething();
}
}
公共类泛型服务:IGenericService
{
私有只读一般存储库;
公共泛型服务(惰性泛型存储库)
{
this.genericRepository=genericRepository;
}
公共无效剂量测定法()
{
genericRepository.Value.DoSomething();
}
}
简单注入器中的绑定
container.Register<Lazy<IARepository>>(
() => new Lazy<IARepository>(container.GetInstance<ARepository>));
container.Register<Lazy<IBRepository>>(
() => new Lazy<IBRepository>(container.GetInstance<BRepository>));
container.Register<Lazy<IAService>>(
() => new Lazy<IAService>(container.GetInstance<AService>));
container.Register(
()=>newlazy(container.GetInstance));
集装箱。登记(
()=>newlazy(container.GetInstance));
集装箱。登记(
()=>newlazy(container.GetInstance));
这里回答了这个问题:
tl;博士:
编译器无法知道在这种情况下,Bar
可以强制转换为Bar
,因为这通常不是真的。您必须通过向中间的对象引入强制转换来“欺骗”:
返回新的FooBar((Bar)(object)Bar)代码>
因此,您应该能够通过以下方式实现这一目标:
public-AService(Lazy-aRepository):基((Lazy)(object)aRepository){
可能重复:另请参见无法将类型为“System.Lazy1[Services.Repos.IARepository]”的对象强制转换为类型为“System.Lazy
1[Services.Repos.IGenericRepository]”。