Resharper认为类型转换是多余的,但没有类型转换,代码不起作用

时间:2015-10-21 11:28:37

标签: c# unity3d casting resharper litjson

通过foreach提取JsonData来提取IDictionary枚举,而不是IList一个。

foreach (var jsonEntry in jsonData)

这导致我的代码抛出错误。

InvalidOperationException: Instance of JsonData is not a dictionary
LitJson.JsonData.EnsureDictionary ()
LitJson.JsonData.System.Collections.Specialized.IOrderedDictionary.GetEnumerator ()
LitJson.JsonData.System.Collections.IDictionary.GetEnumerator ()

将对象转换为IList会导致Resharper发出警告“类型转换是多余的。”

foreach (var jsonEntry in jsonData as IList)

为什么Resharper认为演员是多余的?

1 个答案:

答案 0 :(得分:1)

  

为什么Resharper认为演员是多余的?

如果你这么想:

IList是ICollection,IDictionary也是ICollection,因为它们都实现了'ICollection',Resharper的警告应该变得清晰。