据我所知,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
当中断main
的返回并监视test
时,VS2019调试器显示的是Bot 15
而不是bot #15
。
文件还表明:
调试器是否评估此隐式ToString((调用取决于在"工具"/"选项"/"调试"对话框中的用户设置上。
我不知道它指的是什么用户设置。
这在VS2019中不可用吗?还是我没有抓住要点
这里的主要问题是F#编译器静默地发出DebuggerDisplay
属性来覆盖您正在查看的文档中描述的默认行为。因此,单独覆盖ToString
不会改变调试器在调试F#程序时显示的内容。
F#使用此属性来实现自己的纯文本格式。您可以使用StructuredFormatDisplay
调用ToString
来控制这种格式:
[<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()
如果执行此操作,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}"
FWIW,我认为你关于工具/选项/调试设置的问题的直接答案是";在变量窗口中显示对象的原始结构;。但是,此设置与您试图解决的问题并不真正相关。