class Program
{
static void Main(string[] args) {
Check(new Foo());
Check(new Bar());
}
static void Check<T>(T obj) {
// "The type T cannot be used as type parameter..."
if (typeof(T).IsSubclassOf(typeof(Entity<T>))) {
System.Console.WriteLine("obj is Entity<T>");
}
}
}
class Entity<T> where T : Entity<T>{ }
class Foo : Entity<Foo> { }
class Bar { }
このことをコンパイルする適切な方法は何ですか? Entity<T>非ジェネリック クラスからサブクラス化するか、それが成功するかどうかEntityBaseを試してみることができますが、ブロックを乱用したり、型階層を破壊したりtypeof(Entity<>).MakeGenericType(typeof(T))しない方法はありますか?try { } catch { }
Typeのように便利なように見えるGetGenericArguments方法がいくつかありますが、GetGenericParameterConstraintsそれらの使用方法についてはまったくわかりません...