interfaceOnMyType.GetGenericTypeDefinition()
returns you the closed constructed type of the interface which is different from the type returned from
typeof(IBar<>)
Here is the MSDN article on GetGenericTypeDefinition
, and here is a good quote from it explaining how it works:
Given a Type
object representing this constructed type, the GetGenericTypeDefinition
method returns the generic type definition.
Edit (the answer above is correct in some cases but wrong in this one):
I think I may have found it now. The reason that the type comparison is failing is because the Type
returned from myType.GetInterfaces()
is close to but not identical to the type of the interface itself.
According to MSDN:
If you use the BaseType
property to obtain the base type of Derived
, the FullName
property of the resulting Type object returns null
(Nothing in Visual Basic). To get a non-null FullName
, you can use the GetGenericTypeDefinition
method to get the generic type definition.
So I think this is the problem you are seeing. Since base interfaces are retrieved via GetInterfaces
any type retrieved by that call will not have a FullName
(source). Since it does not have a FullName
the types will fail in comparison.
What I orginally wrote would be true if you were comparing the constructed type which you are not. So unfortunately my first answer is dead wrong - I have left it so that the comments left will make sense.