title: .NET Reflection - Results of GetInterfaces() of generic type definition cannot be successfully compared to generic typeof literal
body: <p>I recently had to do some reflection in .NET and stumbled upon a strange behavior while comparing generic type definitions.</p>
  
  <p>I had to decide whether a type is an <code>IDictionary&lt;,&gt;</code> by extracting a <code>Type</code> object from a LINQ <code>Expression</code> so I was unable to use the <code>is</code> operator to decide it. Additionally, I had to ignore the type parameters, in other words, the only thing that was important is whether I was dealing with an <code>IDictionary&lt;,&gt;</code> of any key/value type.</p>
  
  <p>I started out with the following:</p>
  
  <pre><code>// Just a placeholder, in my code I have no way of knowing that it
  // is a Dictionary of string keys and string values or even that it
  // is a dictionary at all.
  typeof(Dictionary&lt;string, string&gt;)
      .GetGenericTypeDefinition()
      .GetInterfaces()
      .Any(i =&gt; i == typeof(IDictionary&lt;,&gt;))
  </code></pre>
  
  <p>I assumed that since I was reading the interfaces of a generic type definition I would get generic interface definitions. Even more strange is that when I pasted the above code to LINQPad it returned the following:</p>
  
  <pre><code>typeof(IDictionary&lt;TKey,TValue&gt;) // it appears here
  typeof(ICollection&lt;KeyValuePair&lt;TKey,TValue&gt;&gt;) 
  typeof(IEnumerable&lt;KeyValuePair&lt;TKey,TValue&gt;&gt;) 
  typeof(IEnumerable) 
  typeof(IDictionary) 
  typeof(ICollection) 
  typeof(IReadOnlyDictionary&lt;TKey,TValue&gt;) 
  typeof(IReadOnlyCollection&lt;KeyValuePair&lt;TKey,TValue&gt;&gt;) 
  typeof(ISerializable) 
  typeof(IDeserializationCallback) 
  </code></pre>
  
  <p>Yet for some reason the comparison in the <code>Any</code> method did not succeed for any of these elements. However, if I get the generic type definitions for the interfaces themselves like so:</p>
  
  <pre><code>typeof(Dictionary&lt;string, string&gt;)
      .GetGenericTypeDefinition()
      .GetInterfaces()
      .Select(i =&gt; i.IsGenericType ? i.GetGenericTypeDefinition() : i)
      .Any(i =&gt; i == typeof(IDictionary&lt;,&gt;))
  </code></pre>
  
  <p>then it returns <code>true</code> like it should.</p>
  
  <p>Why is that? Aren't the interfaces returned by the <code>.GetInterfaces()</code> method when called on a generic type definition generic interface definitions themselves already? If so, what's the explanation that when I inspect the returned interfaces in LINQPad they appear to be generic type definitions?</p>
  
  <p>I guess it has something to do with the fact that the interface's type parameters could be completely independent from the implementer's generic arguments, e.g.:</p>
  
  <pre><code>public class MyGenericType&lt;TKey, TValue&gt; : ISomeInterface&lt;object&gt; ...
  </code></pre>
  
  <p>but still it looks strange that the returned <code>Type</code> objects don't seem to reflect such a case (as I'd expect in this specific example) and from the 'looks' they appear to be generic type definitions yet they aren't identified as that.</p>