c#字典类,Dictionary<T,M>的改版
Dictionary<T,M>是很好用,但实际在用时,它实在是太长了,而且通常,我们只会用到string的key,而且希望它能返回我们想要的类型值,那就写个范型吧:
public class JnDictionary : Dictionary<string, object> {
public T GetValue<T>(string key) where T : IConvertible {
return (T)Convert.ChangeType(this[key], typeof(T));
}
}
然后,使用 JnDictionary,
参数使用:void func1( JnDictionary dict)
调用func1:func1(new JnDictionary(){ { "key1" : "val1" }, { "key2", "val2" } })
取值:
var dict=new JnDictionary(){ { "key1" : false }, { "key2", 100 }, { "key3" , "jfjef" } };
bool key1=dict.GetValue<bool>("key1");
int key2=dict.GetValue<int>("key2");