增强了受歧视联合的调试信息

RMi*_*330 3 f#

据我所知,这DebuggerDisplayAttribute不能适用于受歧视工会的个人级别,只能适用于顶层阶级。

相应的文档表明重写该ToString()方法是另一种方法。

以下面的例子为例:

type Target =
    | Output of int
    | Bot of int

    override this.ToString () =
        match this with
        | Output idx -> $"output #{idx}"
        | Bot idx -> $"bot #{idx}"

[<EntryPoint>]
let main _ =    
    let test = Bot 15
    
    0
Run Code Online (Sandbox Code Playgroud)

当中断返回main并打开手表时test,VS2019 调试器显示Bot 15而不是bot #15.

该文档还表明:

调试器是否评估此隐式 ToString() 调用取决于“工具”/“选项”/“调试”对话框中的用户设置。

我无法弄清楚它指的是什么用户设置。

这在 VS2019 中不可用还是我只是错过了重点?

bri*_*rns 5

这里的主要问题是 F# 编译器默默地发出一个DebuggerDisplay属性来覆盖您正在查看的文档中描述的默认行为。如此压倒一切ToString单独覆盖不会改变调试器在调试 F# 程序时显示的内容。

F# 使用此属性来实现其自己的纯文本格式。您可以通过使用StructuredFormatDisplayto callToString来控制此格式:

[<StructuredFormatDisplay("{DisplayString}")>]
type Target =
    | Output of int
    | Bot of int

    override this.ToString () =
        match this with
        | Output idx -> $"output #{idx}"
        | Bot idx -> $"bot #{idx}"

    member this.DisplayString = this.ToString()
Run Code Online (Sandbox Code Playgroud)

如果执行此操作,Visual Studio 调试器将显示"bot #15"根据您的需要显示 。

DebuggerDisplay另一种选择是在顶层明确使用自己,正如您提到的:

[<System.Diagnostics.DebuggerDisplay("{ToString()}")>]
type Target =
    | Output of int
    | Bot of int

    override this.ToString () =
        match this with
        | Output idx -> $"output #{idx}"
        | Bot idx -> $"bot #{idx}"
Run Code Online (Sandbox Code Playgroud)

FWIW,我认为您有关工具/选项/调试设置的问题的直接答案是“在变量窗口中显示对象的原始结构”。但是,此设置与您要解决的问题并不真正相关。