C# 使用泛型作为容器类型的接口类型将实现的完整性映射到字典

C# 使用泛型作为容器类型的接口类型将实现的完整性映射到字典,c#,generics,C#,Generics,出发点是,我有一个与泛型的接口,有两个实现: public class Type1 { } public class Implementation1 : IFoo<Type1, int> { public int Method1(Type1 item) { throw new NotImplementedException(); } public Type1 Method2(int id) { throw n

出发点是,我有一个与泛型的接口,有两个实现:

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()
    {

    }
}
接口:

interface IFoo<T , U>
{
    U Method1(T item);
    T Method2(U id);
}
填写字典:

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}
        };
    }
编辑:

更正了通读中的所有拼写错误,发现完整的编译代码段不需要执行,而是能够理解并遵循我试图解释的问题:

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}
            };
        }
    }
}

如果您试图在这里定义一个接口来以多态方式保存这些类型,并且您能够定义一个完全工作的、非通用的接口来提供所有必需的功能,那么您的处理方法是错误的

不要写这个绝对错误、容易出错、目光短浅的怪物,当你需要更多的类型时会发生什么,不管它们是什么?您正在为哪个接口实现相同的命名函数?等等:

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

接口的所有实现都没有发布


当然,如果您可以从代码中提取非泛型接口,就可以这样做。这是任何人的猜测,因为您肯定根本没有提供相关和/或正确的代码。

我不明白您为什么需要泛型,这些泛型类型根本没有在您的代码中使用。此外,您的代码中充斥着语法错误、缺少返回类型、类级别的赋值等。这使得您甚至很难猜测您试图在这里造成的混乱。我知道问题可能有点复杂。我试着只发布有意思的代码,并尽可能简化。我将尽可能多地修改示例代码片段,以避免输入错误。泛型已经存在于场景中,可以假设,除了它们的适当使用之外,它们是各自类型实现的一部分,在那里使用,并且是遇到的场景中问题的一部分。感谢您的commnet:as转换如果对象没有实现您想要转换到的类型,那么将返回null。这叫做安全铸造。从您的示例来看,Implementation1和Implementation2都没有实现IContainerInterfacegeneric类型在您的代码中根本没有使用所有接口的实现都没有发布,但在其余的实现中使用了泛型,但是,如果要在每个实现上调用相同的方法,即它具有相同的名称、参数和返回类型,然后将该方法提取到非泛型接口中,则发布代码的这一部分并不能深入了解问题。如果要调用具有不同签名的方法,则需要担心更大的问题。你会如何调用它或者如何处理返回的值。定义一个接口来保存这些类型这正是我想要做的。我将试试你的建议。Thankstypos更正并编译提供的代码段似乎我的错误是我试图将接口向下扩展到层次结构,而不是像您建议的那样在继承树的根上定义IContainerInterface。这对我来说是一个关键概念,因为我没有理解接口和各自实现之间的绑定,也没有理解接口类型和各自实现的持有者的定义。你从所有的怪事中抓住了我问题的关键,非常感谢通用对象专门化您的具体类型是规则!
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}
            };
        }
    }
}
interface IContainerInterface: IFoo<Type1, int>, IFoo<Type2, int>