简体   繁体   English

获得带有通用参数的接口的具体实现

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

相关问题 如何通过泛型类型获得接口的具体实现? - How to get the concrete implementation of an interface by the generic type? 当将接口用作方法的通用参数时,如何访问具体接口实现的值 - How to access values of a concrete interface implementation, when using the interface as a generic parameter to a method 在Ninject中寻找通用接口的具体实现 - Finding a Concrete Implementation of a Generic Interface in Ninject 将具体实现类型转换为通用接口类型 - Convert concrete implementation type to generic interface type Unity 3.5:通过其开放的通用接口检索具体实现 - Unity 3.5: Retrieve concrete implementation by its open generic interface 将具体实现作为通用返回 - Returning a concrete implementation as a generic 每个类实现的接口方法的强制参数为具体类型 - Force parameter of interface method to be a concrete type per class implementation 泛型参数-使用具体类型进行编译,但是实现的接口不会编译 - Generic parameter - using a concrete type compiles, but an implemented interface does not 如何将接口的具体实现传递到接受通用类型接口的MVC局部视图中? - How can I pass a concrete implementation of an interface into a MVC Partial View that accepts an Interface of generic type? 获取具体类型并创建接口类型参数的实例? - Get a concrete type and create instance of interface type parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM