How to get an IEnumerable<DictionaryEntry> out of an IDictionary?
<p>I essentially want to do this:</p>

<pre><code>if(obj is IDictionary)
{
    return "{" + string.Join(Environment.NewLine, ((IDictionary)obj).Cast&lt;DictionaryEntry&gt;().Select(e =&gt; string.Format("  {0}: {1}", PrettyString(e.Key), PrettyString(e.Value)))) + "}";
}
</code></pre>

<p>But I keep getting an invalid cast. I can do </p>

<pre><code>foreach(DictionaryEntry e in (IDictionary)obj)
</code></pre>

<p>So why can't I do this?</p>

<hr>

<p>I find this quite curious. I wrote an extension to solve the problem:</p>

<pre><code>static class EnumerableExt
{
    public static IEnumerable&lt;DictionaryEntry&gt; Entries(this IDictionary dict)
    {
        foreach (var item in dict) yield return (DictionaryEntry)item;
    }
}
</code></pre>

<p>Then I can just do:</p>

<pre><code>((IDictionary)obj).Entries().Select(...)
</code></pre>

<p>The curious part is that Resharper tells me I can replace my extension with this:</p>

<pre><code>return dict.Cast&lt;DictionaryEntry&gt;();
</code></pre>

<p>But that throws an exception. Is this a bug in Resharper? Or how <code>Cast</code> works? I'd imagine <code>Cast</code> would work exactly the way my extension does.</p>

<p><strong>Edit:</strong> Ah.. I read <a href="http://stackoverflow.com/a/7683336/65387">this answer</a> a bit more closely. Still strange.</p>
