将IDictionary转换为Dictionary

时间:2011-06-21 07:57:33

标签: c# generics dictionary

我必须将System.Collections.Generic.IDictionary<string, decimal>转换为System.Collections.Generic.Dictionary<string, decimal>,我不能。我尝试了ToDictionary方法,无法指定正确的参数。

我尝试了以下内容:

// my dictionary is PlannedSurfaces (of type IDictionary<string, decimal>)
blabla.ToDictionary<string, decimal>(localConstruction.PlannedSurfaces) 

3 个答案:

答案 0 :(得分:59)

var newDict = new Dictionary<string, decimal>(oldDictionary)

答案 1 :(得分:20)

或在linq

var castedDico = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

答案 2 :(得分:0)

var newDictionary = (IDictionary<string, boolean>)ex.Data;

IDictionary 可以转换为 Dictionary

注意,如果字典为空/null,则会抛出异常。