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 の制限により不可能であるとのことでした。しかし、これを行う正しい方法は何でしょうか?