I am writing some base class code to cache objects that looks sort of like the following:
Dictionary<Type, Dictionary<int, BaseClass>> cachedObjects = new Dictionary<Type, Dictionary<int, A>>;
private Dictionary<int, T> GetCacheDictionary<T>() where T : BaseClass
{
if(cachedObjects.ContainsKey(typeof(T)))
{
return (Dictionary<int, T>) cachedObjects[typeof(T)];
}
else
{
Dictionary<int, T> myDictionary = new Dictionary<int, T>();
cachedObjects.Add(typeof(T), myDictionary);
return myDictionary;
}
}
After doing some Google searching, I realized I can't do it this way due to covariance, so I was wondering if anyone had any ideas on how do accomplish this while still using generics and not writing additional support classes.