我试图写一个扩展方法,将数据插入定义词典的词典如下:的字典
items=Dictionary<long,Dictionary<int,SomeType>>()
我至今是:
public static void LeafDictionaryAdd<TKEY1,TKEY2,TVALUE>(this IDictionary<TKEY1,IDictionary<TKEY2,TVALUE>> dict,TKEY1 key1,TKEY2 key2,TVALUE value)
{
var leafDictionary =
dict.ContainsKey(key1)
? dict[key1]
: (dict[key1] = new Dictionary<TKEY2, TVALUE>());
leafDictionary.Add(key2,value);
}
但编译器不喜欢它。声明:
items.LeafDictionaryAdd(longKey, intKey, someTypeValue);
给我一个类型推断错误。
对于声明:
items.LeafDictionaryAdd<long, int, SomeType>(longKey, intKey, someTypeValue);
我得到” ......不包含一个定义......和最佳推广方法重载有一些无效参数
我在做什么错。 ?
这为我工作,当我试图它:| – mquander 2009-05-26 13:19:35
Argh。多么痛苦。根本没有mods? – spender 2009-05-26 13:21:02
哦,我把我的“项目”声明为IDictionary的IDictionary - 这可能是关键的区别。 – mquander 2009-05-26 13:24:44