title: How to get value list from generic dictionary
body: <p>I've used this <a href="http://www.c-sharpcorner.com/uploadfile/rmcochran/genericidenetiymapobjectpool01262008090822am/genericidenetiymapobjectpool.aspx?login=true&amp;user=kfinke" rel="nofollow noreferrer">ObjectPool</a> class as the basis of my Identity Map.  However, I need to bring back the list of all objects of a type.  Matthew has :</p>
  
  <pre><code>    public IEnumerable&lt;T&gt; GetItems&lt;T&gt;()
      {
          Type myType = typeof(T);
  
          if (!m_pool.ContainsKey(myType))
              return new T[0];
  
          return m_pool[myType].Values as IEnumerable&lt;T&gt;;
      }
  </code></pre>
  
  <p>However, when I make changes to the client like so:</p>
  
  <pre><code>        pool.AddItem&lt;Animal&gt;(dog.ID, dog);
          pool.AddItem&lt;Vegetable&gt;(carrot.Identifier, carrot);
          pool.AddItem&lt;Vegetable&gt;(greenbean.Identifier, greenbean);
          pool.AddItem&lt;Mineral&gt;(carbon.UniqueID, carbon);
  
          Console.WriteLine("Dog is in the pool -- this statement is " + pool.ContainsKey&lt;Animal&gt;(dog.ID));
  
          IEnumerable&lt;Vegetable&gt; veggies = pool.GetItems&lt;Vegetable&gt;();
          foreach(Vegetable veg in veggies)
              Console.WriteLine(string.Format("{0} - {1}", veg.Color, veg.IsTasty));
  </code></pre>
  
  <p>veggies is null.  It would appear that m_pool[myType].Values does not support the cast to IEnumerable.</p>
  
  <p>I've tried a few things, for example:</p>
  
  <pre><code>        IDictionary&lt;int, T&gt; dic = (IDictionary&lt;int, T&gt;) m_pool[myType];
          ICollection&lt;T&gt; values = (ICollection&lt;T&gt;)dic.Values;
  </code></pre>
  
  <p>However, I always end up with a casting error.  </p>
  
  <p>What am I missing?</p>