When I have two non-generic Type objects a and b, I can easily verify whether a is assignable from b by using the a.IsAssignableFrom(b) function.
class ClassBase { }
class ClassDerived : ClassBase { }
...
typeof(ClassBase).IsAssignableFrom(typeof(ClassDerived)) //returns true
Now say I have two generic interfaces:
interface IBase<T> { }
interface IDerived<T> : IBase<T> { }
If I close them down, I can do the same thing as before, with exactly the same behaviour, e.g.
typeof(IBase<object>).IsAssignableFrom(typeof(IDerived<object>)) //returns true
In fact, any T that can be used to close down IDerived can also be used to close down IBase and IBase<T> is assignable from IDerived<T> (for that particular T).
However,
typeof(IBase<>).IsAssignableFrom(typeof(IDerived<>)) //returns false
I sort of have an idea why that might be so (they can be closed down on different types and thus get inconvertible?). I understand that a function that does return true in this case is thus somewhat different. The question is: "Is IBase<T> assignable from IDerived<T> for every valid T?" (thanks to hvd)
What I thought of doing is closing the generics and then asking whether they are assignable. But to be general, I would need to close down under the most generic type(s) b can take and that could be a) ugly, b) quite hard to do.
Another approach is to go up the implementation/inheritance tree on b and try comparing it to a.
My question is whether there is an easier way of doing this even in general cases.
Motivation: general interest, as I don't actually need this at the end. However, the initial need for this came while I was using Ninject with open and closed generics and I needed to resolve whether an open generic class can be cast to an open generic interface (class).