title: C# Casting of Dictionary
body: <p>I have this function:</p>
  
  <pre><code>void WriteMap(object value)  
  {  
      IDictionary&lt;string, object&gt; vv = (IDictionary&lt;string, object&gt;)value;  
      foreach (KeyValuePair&lt;string, object&gt; obj in vv)  
      {  
          (write key)  
          (write value)  
      }  
  }  
  </code></pre>
  
  <p>It works if value is really of type <code>IDictionary&lt;string, object&gt;</code>, but it could be anything like:<br>
      <code>IDictionary&lt;string, int&gt;</code>
      <code>IDictionary&lt;string, MyClass&gt;</code>
      <code>IDictionary&lt;string, string[]&gt;</code>
  etc.  </p>
  
  <p>The cast throws a run-time exception. The function does not modify the container, it only prints the keys and values. Any ideas on how I can make this work? Any help would be greatly appreciated.</p>
  
  <p>Thanks!</p>