将接口类型与泛型一起用作容器类型,以将实现的实例映射到字典 [英] Use an Interface type with generics as container type to map intances of the implementation to dictionary

查看:51
本文介绍了将接口类型与泛型一起用作容器类型,以将实现的实例映射到字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出发点是,我有一个带有2种实现的泛型接口:

Departing point is that I have an interface with generics with 2 implementations:

接口:

interface IFoo<T , U>
{
    U Method1(T item);
    T Method2(U id);
}

实施:

public class Type1 { }

public class Implementation1 : IFoo<Type1, int> {
    public int Method1(Type1 item)
    {
        throw new NotImplementedException();
    }

    public Type1 Method2(int id)
    {
        throw new NotImplementedException();
    }

    //....
    public int Method3()
    {
        int myReturnedInt = 0;
        //....

        return myReturnedInt;
    }

   

    public void Method4()
    {

    }
}
 
public class Type2 { }

public class Implementation2 : IFoo<Type2, int> {
    public int Method1(Type2 item)
    {
        throw new NotImplementedException();
    }

    public Type2 Method2(int id)
    {
        throw new NotImplementedException();
    }

    public int Method3()
    {
        int myReturnedInt = 0;
        //....

        return myReturnedInt;
    }

    public void Method4()
    {

    }
}

有了这个出发点,假设我可以获得每种实现类型的实例,我想将每个实例映射到字典中,以访问各自的 Method3 Method4 分别,所以我尝试了:

With this departing point, supposed that I got my instances of each of the implementation types available, I would like to map each instance in a dictionary, to acces to the respective Method3 and Method4 respectively, so I tried:

将泛型的类型和摘要归为一类

Group both of the types and abstract from generics:

public interface IContainerInterface : IFoo<Type1, int>, IFoo<Type2, int>
    {
        int Method3();
        void Method4();
    }

将字典填充到以下位置:

Fill the dictionary in:

public enum EnumType
    {
        type1,
        type2
    }

    Implementation1 type1Instance;
    Implementation2 type2Instance;

    private Dictionary<EnumType, IContainerInterface> _container;

    void Main(string[] args)
    {
        _container = new Dictionary<EnumType, IContainerInterface>{
            {EnumType.type1, type1Instance as IContainerInterface},
            {EnumType.type2, type2Instance as IContainerInterface}
        };
    }

但是不起作用.尽管 Implementation1Instance 可用,但词典中的值为空.一个没有泛型的可行示例将是 this的答案问题,当不涉及泛型时,从接口到实现的绑定是在哪里实现的,但是如何在接口中使用泛型来实现呢?

But does not work. Although Implementation1Instance is available, the value in the dictionary is null. A working example of this with no generics would be the answer to this question, where the binding from interface to implementation is achieved when there are no generics involved, but how to achieve this with generics in the interface?

我的目标是能够分别访问如下类型的方法:

My objective is to be able to access to the methods of the types respectively like this:

//for type1
int myInt = _container[EnumType.type1].Method3();
_container[EnumType.type1].Method4();
//for type2
int myInt = _container[EnumType.type2].Method3();
_container[EnumType.type2].Method4();

更正了通读中的所有拼写错误,并找到了完整的摘录片段(不是要执行,而是要能够理解并跟踪我试图解释的问题):

Corrected all the typos in the read through and find the full compiling snippet (not to be executed, but to be able to understand and follow along what the problem Im trying to explain):

using System;
using System.Collections.Generic;

namespace ConsoleApp10 {

    public interface IFoo<T, U>
    {
        U Method1(T item);
        T Method2(U id);
    }

    public class Type1 { }

    public class Implementation1 : IFoo<Type1, int>
    {

        public int Method1(Type1 item)
        {
            throw new NotImplementedException();
        }

        public Type1 Method2(int id)
        {
            throw new NotImplementedException();
        }

        //....
        public int Method3()
        {
            int myReturnedInt = 0;
            //....

            return myReturnedInt;
        }

       

        public void Method4()
        {

        }
    }

    public class Type2 { }

    public class Implementation2 : IFoo<Type2, int>
    {
        public int Method1(Type2 item)
        {
            throw new NotImplementedException();
        }

        public Type2 Method2(int id)
        {
            throw new NotImplementedException();
        }

        public int Method3()
        {
            int myReturnedInt = 0;
            //....

            return myReturnedInt;
        }

        public void Method4()
        {

        }
    }

    class Program
    {
        public interface IContainerInterface : IFoo<Type1, int>, IFoo<Type2, int>
        {
            int Method3();
            void Method4();
        }

        public enum EnumType
        {
            type1,
            type2
        }

        Implementation1 type1Instance;
        Implementation2 type2Instance;

        private Dictionary<EnumType, IContainerInterface> _container;

        void Main(string[] args)
        {
            _container = new Dictionary<EnumType, IContainerInterface>{
                {EnumType.type1, type1Instance as IContainerInterface},
                {EnumType.type2, type2Instance as IContainerInterface}
            };
        }
    }
}

推荐答案

如果您要在此处定义的只是定义一个接口以多态地保存这些类型,那么您就可以定义一个完全可用的接口,提供所有必需功能的非通用接口,那么您将以错误的方式处理此问题.

If what you're trying to do here is to is to define an interface just to hold these types polymorphically, and you are able to define a fully working, non-generic interface that provides all the required functionality, then you're handling this the wrong way around.

与其编写这种绝对错误的,容易出错的,短视的怪兽(当您需要更多类型时,会发生什么,无论它们是什么?您要为同一个函数实现同名功能?等等),

Instead of writing this absolutely wrong, error-prone and short sighted monstrosity (what happens when you need more types, whatever they are? which interface are you implementing the same-named functions for? etc etc):

interface IContainerInterface: IFoo<Type1, int>, IFoo<Type2, int>

您应该做的是在继承树的最根处定义一个 IContainerInterface ,然后从中派生 IFoo<> .您的字典初始化和用法保持不变,并且您不再需要在每次需要新类型时都更改接口.

What you should do is define an IContainerInterface at the very root of the inheritance tree, then derive IFoo<> from it. Your dictionary initialization and usage remains identical, and you no longer need to change the interface every time you need a new type.

所有接口的实现未发布

All the implementation of the interfaces is not posted

当然,这就是您可以从代码中提取非通用接口的原因.这是任何人的猜测,因为您当然根本不提供相关和/或正确的代码.

Of course, that's if you can extract a non-generic interface from your code. It's anyone's guess, because you certainly don't provide relevant and/or correct code at all.

这篇关于将接口类型与泛型一起用作容器类型,以将实现的实例映射到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆