What's your end goal exactly?
My hope is to be able to get objects doing something like
Texture2D texture = listOfObjects<Texture2D>("nameOfTexture");
SpriteFont font = listOfObjects<SpriteFont>("nameOfFont");
// etc.
That's exactly what XNA's ContentManager does.
Texture2D texture = Content.Load<Texture2D>("nameOfTexture");
SpriteFont font = Content.Load<SpriteFont>("nameOfFont");
Why not just use that?
The ContentManager is used to Load objects, not get them once they've been loaded. I don't think I'd be creating new variables for each and every texture and so on. A Dictionary seems more appropriate.
As for the generic solution I was after, I've come up with this:
Dictionary<Type, object> dict;
public void Add<T>(string key, T value)
{
Type type = typeof(T);
bool dictContainsKey = dict.ContainsKey(type);
if (!dictContainsKey)
dict.Add(type, new Dictionary<string, T>());
Dictionary<string, T> nestedDict = dict[type] as Dictionary<string, T>;
if (nestedDict.ContainsKey(key))
nestedDict[key] = value;
else
nestedDict.Add(key, value);
}
public T Get<T>(string key)
{
Type type = typeof(T);
bool dictContainsKey = dict.ContainsKey(type);
if (!dictContainsKey)
return default(T);
Dictionary<string, T> nestedDict = dict[type] as Dictionary<string, T>;
if (nestedDict.ContainsKey(key))
return nestedDict[key];
else
return default(T);
}
What are your thoughts - is this a bad / unnecessary implementation?
Admittedly it's a little messy, and probably unnecessary. For whatever reason I felt a single Dictionary with all assets is the way to go. My plan was to then write getters and setters and so on for accessing specific types, such as GetTexture(string key) { return // ... gets Texture2D from nested dictionary in dict }
Anyway, I guess having Dictionaries for each type seems to be a more suitable and cleaner solution.
Thanks everybody for your input.