[英]Get Concrete Implementation of Interface with Generic Parameter
I'm feeling pretty dumb right now. 我现在感觉很傻。 Not sure why I can't solve this.
不知道为什么我不能解决这个问题。 I have a repository interface:
我有一个存储库接口:
public interface ICollateralItemBaseImplementation<T> where T : CollateralItemBase
{
int Add(T collateralItem);
T Get(int collateralID);
}
and many implementations, each implementing one of the 10 models that inherit from CollateralItemBase
. 以及许多实现,每个实现都实现了从
CollateralItemBase
继承的10个模型之一。 For example: 例如:
internal sealed class CollateralItemCertifiedDepositRepository : ServiceBaseRepository, ICollateralItemBaseImplementation<CollateralItemCertifiedDeposit>
{
int Add(CollateralItemCertifiedDeposit collateralItem) { /*...*/ }
CollateralItemCertifiedDeposit Get(int collateralID) { /*...*/ }
}
Now i just need to switch on the incoming CollateralItemBase
type to get the repository I need: 现在,我只需要打开传入的
CollateralItemBase
类型即可获取我需要的存储库:
private ICollateralItemBaseImplementation<???> GetRepository(CollateralItemBase item)
{
switch (item.GetType().Name)
{
case "CollateralItemCertifiedDeposit": return new CollateralItemCertifiedDepositRepository();
//...
}
I just need to figure out what to return from this method, for other methods to act on whichever repository I return. 我只需要弄清楚该方法返回什么,其他方法就可以对我返回的任何存储库执行操作。 How do I refactor this to get it working?
我如何重构它才能使其正常工作? I'm pretty sure I have a covariance/contravariance problem.
我很确定我有一个协方差/相反的问题。 Again, I'm feeling pretty dumb, just drawing a blank.
再次,我感觉很愚蠢,只是画了一个空白。
Thanks. 谢谢。
You could do it in two stages. 您可以分两个阶段进行。 Add a non-generic base interface to
ICollateralItemBaseImplementation
then cast to the generic version. 向
ICollateralItemBaseImplementation
添加一个非通用基本接口,然后转换为通用版本。
public interface ICollateralItemBaseImplementation
{
}
public interface ICollateralItemBaseImplementation<T> : ICollateralItemBaseImplementation
where T : CollateralItemBase
{
int Add(T collateralItem);
T Get(int collateralID);
}
public static class RepositoryFactory
{
public static ICollateralItemBaseImplementation<T> GetRepository<T>(T item)
where T : CollateralItemBase
{
return (ICollateralItemBaseImplementation<T>)GetRepositoryImpl(item);
}
private static ICollateralItemBaseImplementation GetRepositoryImpl<T>(T item)
where T : CollateralItemBase
{
switch (item.GetType().Name)
{
case "CollateralItemCertifiedDeposit":
return new CollateralItemCertifiedDepositRepository();
}
return null;
}
}
internal static class Program
{
static void Main()
{
var repo = RepositoryFactory.GetRepository(new CollateralItemCertifiedDeposit());
Debug.Assert(repo is CollateralItemCertifiedDepositRepository);
}
}
what about a generic return param of base type 基本类型的通用返回参数呢
private ICollateralItemBaseImplementation<T> GetRepository(CollateralItemBase item) where T : ServiceBaseRepository
{
switch (item.GetType().Name)
{
case "CollateralItemCertifiedDeposit": return new CollateralItemCertifiedDepositRepository();
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.