C# Iterating over a dictionary passed as a generic
<p>I am having trouble trying to iterate over a dictionary passed to a function as a generic.  For example, I have a function that loads data from a DB.</p>

<pre><code>public T Load&lt;T&gt;(...)
</code></pre>

<p>This function can be called like so, with which I have no problems;</p>

<pre><code>someclasstype data = Load&lt;someclasstype&gt;(...);

List&lt;someclasstype&gt; data = Load&lt;List&lt;someclasstype&gt;&gt;(...);
</code></pre>

<p>I've recently tried to extend this to be able to deal with dictionaries as well, and calling it like so:</p>

<pre><code>Dictionary&lt;long, someclasstype&gt; data = Load&lt;Dictionary&lt;long, someclasstype&gt;&gt;(...)
</code></pre>

<p>I can load the data without a problem and store it in the dictionary no problem.</p>

<p>At this stage, the dictionary, with all its keyvaluepairs is stored in a variable called result, and I'm creating an IEnumerable with </p>

<pre><code>IEnumerator resultIdx = ((IEnumerable)result).GetEnumerator();
if (!resultIdx.MoveNext())
    return (T)result;

object kvp = resultIdx.Current;
</code></pre>

<p>So far so good.  I can see the value of the key and the value of the value in a watch, or by mouseover on the kvp variable.</p>

<p>But I cannot figure out how to get the value part of the keyvaluepair from kvp.</p>

<pre><code>// None of these work - I get compile time errors, unboxing errors, or invalid cast errors.
object item = ((KeyValuePair&lt;TKey, TValue&gt;)kvp).Value;

object item = ((KeyValuePair&lt;long, object&gt;)kvp).Value;

object item = ((T)kvp).Value // Never had a hope for this, but desperation...
</code></pre>

<p>Does anyone have any idea how I can do this?</p>