Resharper认为typecast是多余的,但没有typecast的代码不工作 [英] Resharper thinks that typecast is redundant, but without typecast the code does not work

查看:356
本文介绍了Resharper认为typecast是多余的,但没有typecast的代码不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过foreach抓取迭代 JsonData IDictionary 枚举而不是 IList

Iterating JsonData through foreach fetches IDictionary enumerator instead of IList one.

foreach (var jsonEntry in jsonData)

这会导致我的代码抛出一个错误。 / p>

This causes my code to throw an error.

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发出警告类型转换是多余的

Casting the object to IList causes Resharper to issue a warning "Type cast is redundant."

foreach (var jsonEntry in jsonData as IList)

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

Why does Resharper think the cast is redundant ?

推荐答案

如果你这样认为:

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

IList is a ICollection and IDictionary is also a ICollection because both of them implement 'ICollection', Resharper's warning should become clear.

这篇关于Resharper认为typecast是多余的,但没有typecast的代码不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆