我无法弄清楚这个的正确语法..我有一个像这样的方法:
public IDictionary<string, object> FindTransactions()
{
Dictionary<string, object> returnItems = new Dictionary<string, object>();
//do stuff
return returnItems;
}
我想将其返回值分配给我的消费代码中的新变量。这些不太正确:
IDictionary returnItems = FindTransactions();
//Cannot implicitly convert type 'System.Collections.Generic.IDictionary<string, object>' to 'System.Collections.IDictionary'. An explicit conversion exists (are you missing a cast?)
Dictionary<string, object> returnItems2 = FindTransactions();
//Cannot implicitly convert type 'System.Collections.Generic.IDictionary<string, object>' to 'System.Collections.Generic.Dictionary<string, object>'. An explicit conversion exists (are you missing a cast?)
......这是如何正确完成的?
答案 0 :(得分:2)
在这两种情况下你都很接近:
IDictionary<string, object> returnItems2 = FindTransactions();
通用IDictionary<K,V>
接口不会扩展非通用IDictionary
,因此您无法对其进行转换,并且您无法从较少派生的类型中隐式转换(比较接口)到更多派生类型(如实现),因为不能保证实际类型是Dictionary<K, V>