C# 在c中作为泛型类型参数传递泛型#

C# 在c中作为泛型类型参数传递泛型#,c#,generics,type-parameter,C#,Generics,Type Parameter,我在C#中对泛型做了一个小实验,遇到了一个问题,我想将泛型类型作为带有约束的类型参数传递,以实现一个我不知道其类型的泛型接口 以下是我的例子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program {

我在C#中对泛型做了一个小实验,遇到了一个问题,我想将泛型类型作为带有约束的类型参数传递,以实现一个我不知道其类型的泛型接口

以下是我的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        interface IGenericCollection<T>
        {
            IEnumerable<T> Items { get; set; }
        }

        abstract class GenericCollection<T> : IGenericCollection<T>
        {
            public IEnumerable<T> Items { get; set; }
        }

        //This class holds a generic collection but i have to ensure this class
        //implements my IGenericCollection interface. The problem here is that
        //i dont know which type TGenericCollection is using and so i am unable to
        //pass this information to the constraint. 

        class CollectionOwner<TGenericCollection>
           where TGenericCollection : IGenericCollection< dont know ... >
        {
            protected TGenericCollection theCollection = default(TGenericCollection);
        }

        static void Main(string[] args)
        {
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序3
{
班级计划
{
接口IGenericCollection
{
IEnumerable Items{get;set;}
}
抽象类GenericCollection:IGenericCollection
{
公共IEnumerable项{get;set;}
}
//这个类包含一个泛型集合,但我必须确保这个类
//实现我的IGenericCollection接口。这里的问题是
//我不知道TGenericCollection使用的是哪种类型,因此我无法确定
//将此信息传递给约束。
类集合所有者
其中TGenericCollection:IGenericCollection
{
受保护的TGenericCollection theCollection=默认值(TGenericCollection);
}
静态void Main(字符串[]参数)
{
}
}
}

我在这里读过几篇文章,都告诉我这是不可能的,因为C#和CLR的限制。但是正确的方法是什么呢?

也许您应该使用另一个类型参数:

class CollectionOwner<TGenericCollection, T2>
   where TGenericCollection : IGenericCollection<T2>
   where T2 : class
{
    protected TGenericCollection theCollection = default(TGenericCollection);
}
类集合所有者
其中TGenericCollection:IGenericCollection
其中T2:类
{
受保护的TGenericCollection theCollection=默认值(TGenericCollection);
}

这是否适合您的需要?

我认为这里没有问题,只需向您的Owner类添加另一个泛型参数:

 class CollectionOwner<T,TGenericCollection>
           where TGenericCollection : IGenericCollection<T>
        {
            protected TGenericCollection theCollection = default(TGenericCollection);
        }
类集合所有者
其中TGenericCollection:IGenericCollection
{
受保护的TGenericCollection theCollection=默认值(TGenericCollection);
}

您可以将第二个泛型参数添加到实现类中。下面的静态
示例
方法显示了一个示例

public interface ITest<T>
{
    T GetValue();
}

public class Test<T, U> where T : ITest<U>
{
    public U GetValue(T input)
    {
        return input.GetValue();
    }
}

public class Impl : ITest<string>
{
    public string GetValue()
    {
        return "yay!";
    }

    public static void Example()
    {
        Test<Impl, string> val = new Test<Impl,string>();
        string result = val.GetValue(new Impl());
    }
}
公共接口测试
{
T GetValue();
}
公共类测试,其中T:ITest
{
公共U GetValue(T输入)
{
返回input.GetValue();
}
}
公共类Impl:ITest
{
公共字符串GetValue()
{
返回“耶!”;
}
公共静态void示例()
{
测试值=新测试();
字符串结果=val.GetValue(new Impl());
}
}

使用第二个泛型参数是一个选项4,我已经想使用它了,但是这个呢

    abstract class GenericCollection<T> : IGenericCollection<T>
    {
        public IEnumerable<T> Items { get; set; }
    }

    class ConcreteCollection : GenericCollection<string>
    {

    }

    static void Main(string[] args)
    {
       // will constraint fail here ?
       CollectionOwner<int,ConcreteCollection> o = new  CollectionOwner(int, ConcreteCollection);
    }
抽象类GenericCollection:IGenericCollection
{
公共IEnumerable项{get;set;}
}
类ConcreteCollection:GenericCollection
{
}
静态void Main(字符串[]参数)
{
//约束会在这里失败吗?
CollectionOwner o=新的CollectionOwner(int,ConcreteCollection);
}