简体   繁体   English

为什么Resharper将此标记为“访问修改后的闭包”?

[英]Why does Resharper flag this as “access to modified closure”?

I'm sorry if this is a repeat question. 如果这是一个重复的问题,我很抱歉。 I see many questions here about "modified closure", but none seem to address what I'm seeing. 我在这里看到很多关于“修改封闭”的问题,但似乎都没有解决我所看到的问题。

Resharper 2016.2 is flagging both my use of "b" and "i1" inside the lambda expression as "access to modified closure". Resharper 2016.2标记我在lambda表达式中使用“b”和“i1”作为“访问修改后的闭包”。 I don't think it should. 我认为不应该。 I never use either b or i or i1 anywhere else in the program. 我从不在程序中的任何其他地方使用b或i或i1。 Both are declared within the loop -- in fact, i1 was created by Resharper to resolve the modified closure issue with i. 两者都在循环中声明 - 实际上,i1是由Resharper创建的,用i来解决修改后的闭包问题。 I can try to resolve it with i1 and it will just create an "int i2 = i1" and still give me the warning on i2! 我可以尝试用i1解决它,它只会创建一个“int i2 = i1”并仍然在i2上给我警告! Surely this is not right. 当然这是不对的。 What am I missing here? 我在这里错过了什么?

for (int i = 0; i < 10; ++i) {
    Button b = new Button();
    int i1 = i;
    Invoker.SyncInvoke(b, () => { b.Text = "number " + i1; });
}

EDIT: This has to be a bug. 编辑:这必须是一个错误。 At one point in a certain source file (at line 424, to be precise) are these three lines: 在某个源文件中的某一点(准确地说是第424行)是这三行:

        var collapsed = daSheet;
        int numrows = collapsed.GetLastNonEmptyRow(NonEmptyItemFlag.Data);
        int numcols = collapsed.GetLastNonEmptyColumn(NonEmptyItemFlag.Data);

If I paste the above snippet (the for loop above) BEFORE those lines, I get no warning. 如果我在这些行之前粘贴上面的代码片段(上面的for循环),我没有得到任何警告。 If I paste it AFTER those lines, I get a warning about both "b" and "i1". 如果我在这些行之后粘贴它,我会收到关于“b”和“i1”的警告。 If I paste it between the 2nd and 3rd lines, I get the warning about "i1", but not b. 如果我将它粘贴在第2行和第3行之间,我会收到关于“i1”的警告,但不是b。 That makes no sense. 这是没有意义的。

" the false positive rate for some of their analyzers is pretty terrible" - your're right^^ “他们的一些分析仪的误报率非常糟糕” - 你说对了^^

In fact Resharper is not that wrong. 事实上,Resharper并没有错。

Imagine instead of Invoker... you would use sth like this Task.Startnew(...) I would assume(with only reading the source) that after executing you don't have 10 different buttons, you have only one and this one has the text "number 9". 想象一下,而不是Invoker ...你会使用像这样的Task.Startnew(...)我会假设(只读取源代码)执行后你没有10个不同的按钮,你只有一个和这一个有“9号”字样。

Because the compiler brings the button and the i1 parameter within the lambda expression close together, it's worth it in general to have a look into the decompiled source code with dotpeek ? 因为编译器将lambda表达式中的按钮和i1参数放在一起,所以通常用dotpeek来查看反编译的源代码是值得的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM