1

Resharper 的Access to Modified Closure警告通常很有帮助。我只是注意到,当我在Anyfor each 循环中调用该方法时,如果我不使用左括号和右括号,我会收到警告。一旦我添加(),错误就会消失。

错误本身是否消失了,或者我只是不小心欺骗了 Resharper 的静态代码分析检测。

Dim groupExists as Boolean

For each oldPerson in oldData

    'access to modified closure warning on oldPerson.groupId
    groupExists = (From newPerson In newData 
                   Where newPerson.GroupId = oldPerson.groupId).Any

    'no closure problem reported
    groupExists = (From newPerson In newData 
                   Where newPerson.GroupId = oldPerson.groupId).Any()

Next

当然,我可以通过将以下代码放入For Each循环中并将 与newPerson.GroupId本地声明的变量进行比较来解决此问题。

'declare locally to avoid access to modified closure
Dim groupId as Integer = person.groupId
4

1 回答 1

3

我认为这是一个 Resharper 错误,我会在那里提交。在立即执行的 linq 语句中不能有修改的闭包,就像这样Any做一样。

VB 不是我的“母语”,但括号不应该在 VB.Net 中产生任何影响(就像在 VB6 中所做的那样)。

于 2013-05-11T14:36:54.660 回答