lundi 15 août 2016

How to test if generic arguments in two classes represent the same associated type?

Say I have three classes:

public class Base<T> {}
public class DerivedGeneric<T> : Base<T> {}
public class DerivedSpecificIntAndNewGeneric<T> : Base<int> {}

Here, the T in DerivedGeneric<T> is the same associated type as the T in Base<T>. However, the T in DerivedSpecificIntAndNewGeneric<T> is not the same associated type as the T in Base<T>.

Background for this: As part of trying to get all assignable types for a given type, I need to check if two T parameters represent the same associated type before it even makes sense to test if they're the same value. (I may be using terminology wrong here; the two generic arguments represent the same what?)

I've checked other posts related to finding derived types of a generic type, like this one, but they disregard the values of the generic arguments completely going by generic type definitions alone, and that's not useful for finding types that can actually be instantiated (with Activator) and assigned. I find that doing it properly is fairly complex, and figuring out of generic arguments represent the same associated type is one of the needed steps.





Aucun commentaire:

Enregistrer un commentaire