views:

1197

answers:

2
var _pool = new Dictionary<Type, Dictionary<EntityIdType, Object>>();

public IEnumerable<EntityType> GetItems<EntityType>()
{
    Type myType = typeof(EntityType);

    if (!_pool.ContainsKey(myType))
        return new EntityType[0];

    //does not work, always returns null
    // return _pool[myType].Values; as IEnumerable<EntityType>;

    //hack: cannot cast Values to IEnumarable directly
    List<EntityType> foundItems = new List<EntityType>();
    foreach (EntityType entity in _pool[myType].Values)
    {
        foundItems.Add(entity);
    }
    return foundItems as IEnumerable<EntityType>;

}
+4  A: 

Try this:

return _pool[myType].Values.Cast<EntityType>();

This has the effect of casting every element in the enumeration.

mquander
+1  A: 

_pool is defined as being of type Dictionary<Type, Dictionary<EntityIdType, Object>>

Because of that, the call to the dictionary returned for a type returns an ICollection<Object> which you can not cast directly to IEnumerble<EntityType>.

Rather, you have to use the Cast extension method, as indicated in the other answer for this question:

http://stackoverflow.com/questions/627991/cannot-cast-dictionary-valuecollection-to-ienumarablet-what-am-i-missing/627999#627999

casperOne