item.GetType() == typeof(KeyValuePair<,>)
The above will never work: it is impossible to make an object of type KeyValuePair<,>
.
The reason is that typeof(KeyValuePair<,>)
does not represent a type. Rather, it is a generic type definition - a System.Type
object used to examine structures of other generic types, but not themselves representing a valid .NET type.
If an item
is, say, a KeyValuePair<string,int>
, then item.GetGenericTypeDefintion() == typeof(KeyValuePair<,>)
Here is how you can modify your code:
...
else if (item.IsGenericType() && item.GetGenericTypeDefintion() == typeof(KeyValuePair<,>)) {
...
}