[英]Passing a generic as generic type parameter in c#
我在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)
{
}
}
}
我在这里看过几篇文章,由于C#和CLR的限制,所有人都告诉我这是不可能的。 但是这样做的正确方法是什么?
也许你应该另一个类型参数:
class CollectionOwner<TGenericCollection, T2>
where TGenericCollection : IGenericCollection<T2>
where T2 : class
{
protected TGenericCollection theCollection = default(TGenericCollection);
}
这适合你需要吗?
我不认为这里有问题,只需在您的Owner类中添加另一个泛型参数:
class CollectionOwner<T,TGenericCollection>
where TGenericCollection : IGenericCollection<T>
{
protected TGenericCollection theCollection = default(TGenericCollection);
}
您可以将第二个通用参数添加到实现类。 下面的静态Example
方法显示了此示例。
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());
}
}
使用第二个通用参数是一个选项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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.