title: Get key values of a generic Dictionary <,> using reflection
body: <p>How can I get the key values of a generic <code>IDictionary&lt;,&gt;</code> using reflection.</p>
  
  <p>This is the type of thing I want to do.</p>
  
  <pre><code>public static string Format&lt;T&gt;(T item)
  {
      if (item.GetType().GetInterfaces().Any(x =&gt; x.IsGenericType &amp;&amp; x.GetGenericTypeDefinition() == typeof(IDictionary&lt;,&gt;)))
      {
          // I know it's a IDictionary so figure out what the types are
          Type keyType = item.GetType().GetGenericArguments()[0];
          Type valueType = item.GetType().GetGenericArguments()[1];
  
          //Now cast it to the correct IDictionary.  How do I properly inject the type here?
          var keyList = ((IDictionary&lt;keyType, valueType&gt;)item).Select(x =&gt; x.Key.ToString()).ToArray&lt;string&gt;();
  
      }
  }
  </code></pre>
  
  <p>Edit: Clarified that I want to use reflection</p>