Here’s the problem. I would like to create a class which will contain configuration data. This data consists of key/value pairs. Some examples: “hostName”=”localhost”, “timeout”=1000, etc.. My initial idea was to store all these in a generic dictionary:
private Dictionary<string, ConfigurationItem<object>> Configuration =
new Dictionary<string, ConfigurationItem<object>>();
And the ConfigurationItem class as:
public class ConfigurationItem<T>
{
public string Name { get; set; }
public T Value { get; set; }
}
My hopes are that I can query and set the configuration as follows:
int i = Configuration.Get<int>("Timeout");
Configuration.Set("Timeout", 1000);
Unfortunately, I can’t insert items in the configuration dictionary using the following code:
public void SetValue<T>(string key, ConfigurationItem<T> value)
{
if (Configuration.ContainsKey(key.ToUpper()))
Configuration[key.ToUpper()] = **value**;
else
Configuration.Add(key.ToUpper(), **value**);
}
The bold parts are showing errors:
Cannot convert type ConfigurationItem<T> to ConfigurationItem<Object>
I am using C# 4.0 since I hoped to fixed this problem using the new variance feature, but no luck so far. If more information is needed, just name it! Thanks for your help!
You could just define your Dictionary as private Dictionary<string, object>, and cast the objects before you return them from your Get method.