ReSharper的警告 - 访问修改关闭 [英] ReSharper Warning - Access to Modified Closure

查看:234
本文介绍了ReSharper的警告 - 访问修改关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

string acctStatus = account.AccountStatus.ToString();
if (!SettableStatuses().Any(status => status == acctStatus))
    acctStatus = ACCOUNTSTATUS.Pending.ToString();

注意account.AccountStatus的类型ACCOUNTSTATUS的枚举。在第二行,ReSharper的是给我警告来修改封闭访问acctStatus。当我做推荐的操作,复制到本地变量的,它修改code以下内容:

Note that account.AccountStatus is an enum of type ACCOUNTSTATUS. On the second line, ReSharper is giving me the warning "Access to Modified Closure" for acctStatus. When I do the recommended operation, Copy to local variable, it modifies the code to the following:

string acctStatus = realAccount.AccountStatus.ToString();
string s = acctStatus;
if (!SettableStatuses().Any(status => status == s))
    acctStatus = ACCOUNTSTATUS.Pending.ToString();

为什么这更好或preferable什么我原本?

Why is this better or preferable to what I had originally?

修改

它还建议的裹局部变量数组的,其产生:

It also recommends Wrap local variable in array, which produces:

string[] acctStatus = {realAccount.AccountStatus.ToString()};
if (!SettableStatuses().Any(status => status == acctStatus[0]))
    acctStatus[0] = ACCOUNTSTATUS.Pending.ToString();

这似乎是彻头彻尾的古怪的我。

This seems downright wacky to me.

推荐答案

究其原因,警告是在一个循环中,你可能会访问一个正在改变的变量。然而,修复是不是真的在做这种非循环上下文你什么。

The reason for the warning is that inside a loop you might be accessing a variable that is changing. However, the "fix" isn't really doing anything for you in this non-loop context.

想象一下,你有一个FOR循环和如果是里面和字符串声明是外面。在这种情况下,误差会正确识别敛到某物不稳定的基准的问题。

Imagine that you had a FOR loop and the if was inside it and the string declaration was outside it. In that case the error would be correctly identifying the problem of grabbing a reference to something unstable.

你不想要的一个例子:

string acctStatus

foreach(...)
{
  acctStatus = account.AccountStatus[...].ToString();
  if (!SettableStatuses().Any(status => status == acctStatus))
      acctStatus = ACCOUNTSTATUS.Pending.ToString();
}

的问题是,会被关闭抢acctStatus的引用,但每次循环迭代将会改变该值。在的情况下,它会更好:

foreach(...)
{
  string acctStatus = account.AccountStatus[...].ToString();
  if (!SettableStatuses().Any(status => status == acctStatus))
      acctStatus = ACCOUNTSTATUS.Pending.ToString();
}

作为变量的情况下是循环,一个新的实例将会每次被创建,因为我们已经搬进了当地的环境里面的变量(循环)。

As the variable's context is the loop, a new instance will be created each time because we have moved the variable inside the local context (the for loop).

该建议听起来像是在code的ReSharper的的解析错误。然而,在许多情况下,这是一个有效的关注(如第一个例子,其中该参考,尽管它在一封闭捕获变化)。

The recommendation sounds like a bug in Resharper's parsing of that code. However, in many cases this is a valid concern (such as the first example, where the reference is changing despite its capture in a closure).

我的经验法则是,当有疑问做出局部的。

My rule of thumb is, when in doubt make a local.

下面是一个真实的例子,我被咬伤:

Here is a real world example I was bitten by:

        menu.MenuItems.Clear();
        HistoryItem[] crumbs = policyTree.Crumbs.GetCrumbs(nodeType);

        for (int i = crumbs.Length - 1; i > -1; i--) //Run through items backwards.
        {
            HistoryItem crumb = crumbs[i];
            NodeType type = nodeType; //Local to capture type.
            MenuItem menuItem = new MenuItem(crumb.MenuText);
            menuItem.Click += (s, e) => NavigateToRecord(crumb.ItemGuid, type);
            menu.MenuItems.Add(menuItem);
        }

请注意,我捕捉到的NodeType类型的地方,注意节点类型和HistoryItem crumb.ItemGuid,不屑[I] .ItemGuid。这保证了我的闭合将不必将改变项的引用。

Note that I capture the NodeType type local, note nodeType, and HistoryItem crumb.ItemGuid, not crumbs[i].ItemGuid. This ensures that my closure will not have references to items that will change.

此前使用当地人,这些事件将与目前的值,而不是捕获的值我的预期触发。

Prior to using the locals, the events would trigger with the current values, not the captured values I expected.

这篇关于ReSharper的警告 - 访问修改关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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