dimanche 28 juin 2015

Generic type inheritance

public class BaseGenericType<T>
{
}

public class SubGenericType<T>: BaseGenericType<List<T>>
{
}

I have two generic types above, which one inherits from another but is still generic. The strange thing I can't figure out is that typeof(SubGenericType<>).IsSubclassOf(typeof(BaseGenericType<>)) returns false. And typeof(SubGenericType<>).IsSubclassOf(typeof(BaseGenericType<List<>>)) still returns false. I've tried GetGenericTypeDefinition() and MakeGenericType() and GetGenericArguments() to check the inheritance, still not working. But typeof(SubGenericType<int>).IsSubclassOf(typeof(BaseGenericType<List<int>>)) returns true.

What I want is to get all classes by reflection then grab the specific class which inherits from a generic type passed in.

e.g.

(1)List<int> -->

(2)get generic type definition ==> List<T> -->

(3)make generic ==> BaseGenericType<List<T>> -->

(4)find subclass ==> SubGenericType<T>

(5)make generic ==> SubGenericType<int>

In step (4) I find nothing although I actually have that SubGenericType<T>. Why is that?





Aucun commentaire:

Enregistrer un commentaire