ReSharper 是否会警告字典键?

发布于 2024-12-12 03:30:57 字数 596 浏览 0 评论 0原文

我们最近遇到一个问题,我们调用字典来检索值,并期望键存在。它并没有导致流程中断。

ReSharper 是否具有检查此问题的功能,就像我听说它可以检查空对象一样?

这里有一个例子来支持我所说的内容:

Dictionary<String, Entity> allEntities = 
      new Dictionary<String, Entity>(SringComparer.OrdinalIgnoreCase);

allEntities.AddMany(db.GetAllEntities());

Entity thisEntity = allEntities[entityID]; 
      // <-- error here as EntityID isn't in all entities...

我希望 ReSharper 能够说我没有像这样检查字典:

if (allEntities.ContainsKey(entityID))
    ...

仅供参考,我没有 ReSharper,但这将是另一件事添加到业务案例中以供所有开发人员使用。

We recently had an issue where we called a dictionary to retrieve a value, expecting the key to be present. It wasn't leading to a process breaking.

Does ReSharper have the functionality to check for this like I've heard it can for null objects?

Here is an example to support what I am talking about:

Dictionary<String, Entity> allEntities = 
      new Dictionary<String, Entity>(SringComparer.OrdinalIgnoreCase);

allEntities.AddMany(db.GetAllEntities());

Entity thisEntity = allEntities[entityID]; 
      // <-- error here as EntityID isn't in all entities...

I would like ReSharper to be able to say I haven't checked the dictionary like so:

if (allEntities.ContainsKey(entityID))
    ...

As an FYI, I don't have ReSharper, but this would be one more thing to add in to the business case to get it for all developers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-12-19 03:30:57

即使它可以(我认为它不会),它所做的只是警告您键可能不在字典中 - 就像它警告您一个对象可能为空。您仍然有责任添加代码来检查您是否认为有必要。

我认为,如果这样的功能在每次访问字典时都会向您发出警告,那么它将是一种负担,而不是一种祝福。然后,您可能会争辩说,您希望它就所有其他类型的集合访问或可能引发的任何异常发出警告。这里要传达的信息是,您应该更多地依赖单元测试来捕获此类问题,而不是静态分析工具

编辑:

由于 ReSharper 没有此功能,如果您确实需要它,您可以考虑使用新的功能自己编写它Roslyn API。请参阅这篇文章< /a> 有关如何编写代码分析器的示例

Even if it could (which I don't think it does), all it would do is warn you that the key might not be in the dictionary - just like it warns you that an object might be null. The onus is still on you to add in the code to check if you think it is necessary.

I think that such a feature would be a burden more than a blessing if it warned you about every dictionary access you made. Then you could argue that you wanted it to warn you about every other type of collection access or any and every exception that could possibly be thrown. The take home message here is that you should rely more on unit testing to catch such issues rather than static analysis tools

EDIT:

Since ReSharper doesn't have this functionality, if you really want it, you could think about writing it yourself with the new Roslyn APIs. See this article for an example of how to write a code analyzer

终止放荡 2024-12-19 03:30:57

我目前已经安装了 ReSharper 6,至少在我的设置中,它不会警告我在访问其值之前使用 ContainsKey 检查 Dictionary

I have currently installed ReSharper 6, and at least with my settings, it does not warn me to check a Dictionary with ContainsKey before accessing its value.

别念他 2024-12-19 03:30:57

Resharper是编译时使用的工具。它怎么可能知道在运行时哪些值将被放入字典中?

在对它进行任何操作之前,您可以检查字典以查看它是否具有您想要的值。

Entity thisEntity;
if (allEntities.TryGetValue(entityID, out thisEntity)){
    //DoStuff with thisEntity
}

或者仅使用 if (allEntities.ContainsKey(entityID)){}

Resharper is a tool used at compile time. How could it possibly know what values are going to be put in a dictionary at runtime?

You can check the dictionary to see if it has the value you want before doing anything with it.

Entity thisEntity;
if (allEntities.TryGetValue(entityID, out thisEntity)){
    //DoStuff with thisEntity
}

or just use if (allEntities.ContainsKey(entityID)){}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文