Visual Studio 2022(或 ReSharper)是否可以显示按执行顺序排序的所有成员引用的列表或树?

Can Visual Studio 2022 (or ReSharper) show a list or tree of all references to a member sorted by order of execution?

提问人:JuanF 提问时间:9/14/2023 最后编辑:JuanF 更新时间:9/29/2023 访问量:93

问:

我正在重写一个类中的代码,该类在许多不同的私有方法中更改私有状态,并且很难跟踪该类的每个成员最终将具有什么值。

赋值大多独立于其先前的值以及彼此之间的值,因此许多成员的状态最终由称为 last 的任何赋值决定。它们都是布尔值。

这是一个示例(在 VB 中,但同样适用于 C#),展示了最简单的情况,没有任何分支或具有多个引用的二阶方法。假设该方法是入口点:SetVisibilities

Dim isAVisible As Boolean
Dim isBVisible As Boolean
Dim isCVisible As Boolean

Private Sub SetVisibilities()
    ChangeVisibilitiesFirst()
    ChangeVisibilitiesSecond()
    ' This is the last change for C but is the earliest reference in the file (by line number)
    isCVisible = False
End Sub

Private Sub ChangeVisibilitiesSecond()
    isAVisible = False
    isBVisible = True
    isCVisible = True
End Sub

' This gets called first but is defined later in the file
Private Sub ChangeVisibilitiesFirst()
    isAVisible = True
    isBVisible = False
    isCVisible = True
End Sub

在进一步简化每个可见性评估之前,我想首先安全地将每个成员的所有状态更改转换为这样的内容:

Private Sub SetVisibilities()
    isAVisible = GetVisibilityForA
    isBVisible = GetVisibilityForB
    isCVisible = GetVisibilityForC
End Sub

Private Function GetVisibilityForA() As Boolean
    Dim response As Boolean
    response = True ' From: ChangeVisibilitiesFirst()
    response = False ' From: ChangeVisibilitiesSecond()
    Return response
End Function

Private Function GetVisibilityForB() As Boolean
    Dim response As Boolean
    response = False ' From: ChangeVisibilitiesFirst()
    response = True ' From: ChangeVisibilitiesSecond()
    Return response
End Function

Private Function GetVisibilityForC() As Boolean
    Dim response As Boolean
    response = False ' From: ChangeVisibilitiesFirst()
    response = True ' From: ChangeVisibilitiesSecond()
    response = False ' From: SetVisibilities()
    Return response
End Function

我的手动方法是从入口点 () 开始,然后首先深入到每个方法调用和每行代码,直到我找到一个赋值,然后将该赋值附加到该特定成员的定制新方法。如果涉及一些分支,我也会附加它。SetVisibilities

到目前为止,我已经使用了 Visual Studio 2022 的“查找所有引用”和“查看调用层次结构”,其中第一个按它们在文件中找到的顺序列出了所有引用,虽然调用层次结构很接近,但很难解析,因为它在通常如下所示的树中表示:

- isCVisible
  - ChangeVisibilitiesSecond(), calls isCVisible on line 15
    - SetVisibilities(), calls ChangeVisibilitiesSecond() on line 7
  - SetVisibilities(), calls isCVisible on line 5
  - ChangeVisibilitiesFirst(), calls isCVisible on line 6
    - SetVisibilities(), calls ChangeVisibilitiesFirst() on line 5

虽然我更希望看到的是这样的:

- SetVisibilities()
  - ChangeVisibilitiesFirst()
    - calls isCVisible on line 22
  - ChangeVisibilitiesSecond()
    - calls isCVisible on line 15
  - calls isCVisible on line 9
C# vb.net Visual-Studio Resharper

评论

0赞 JuanF 9/14/2023
我也愿意尝试任何其他能够进行此分析的工具、编辑器、扩展或 IDE。
0赞 Jonathan Dodds 9/14/2023
您可能希望查看可以从代码生成 UML 序列图的工具。
0赞 JuanF 9/14/2023
@Jonathan 那行得通!我正在尝试找到一个适用于 Visual Basic 的,谢谢!如果我找到一个评论,我会编辑这个评论,否则我只会遵循手动深度优先的参考搜索
0赞 Piers Myers 9/15/2023
我会尝试重写代码,使其不那么复杂,并且依赖于运行方法所需的顺序。大多数工具可能更适合 C# 而不是 Visual Studio。ReSharper确实提供了“来电”和“去电”检查,如果这对您有帮助的话。
0赞 JuanF 9/28/2023
@Piers 是的,重写这个类中的代码是这里的目标,像我描述的工具或序列图将极大地帮助确保我不会改变当前的行为。

答: 暂无答案