I’m doing some generic object comparison, using reflection and recursion. The recursive method needs some type information in each step, which is given by the caller. At one point I know that the next property is a Dictionary<T,U>, and I want to send the correct type. I came up with this:
Type dictionaryType = typeof (IDictionary<,>).MakeGenericType(new [] {keyType.PropertyType, typeof(ValueType)});
Where keyType and ValueType are types found out earlier. However, this type does not implement the IDictionary<KeyType, ValueType> interface, according to dictionaryType.GetInterfaces(). Why? It looks like it should…
Because the type IS
IDictionary<KeyType, ValueType>. TheGetInterfacesmethod only promises to returnAn array of Type objects representing all the interfaces implemented or inherited by the current Type. SinceIDictionary<>does not (and indeed cannot) implement itself, the return value is legitimately all interfaces it inherits.Using
Dictionary<,>as an arbitrary class that implementsIDictionary<,>, the following would be more appropriate: