c# - 是否可以在 DebuggerDisplay 中使用条件?

标签 c# .net debugging debuggerdisplay

考虑以下类:

[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}")]
public class FileWrapper
{
    public string FileName { get; set; }
    public bool IsTempFile { get; set; }
    public string TempFileName { get; set; }
}

我想添加一个基于 IsTempFileName 属性的调试器显示。当实例是临时文件时,我想添加字符串 , TempFileName = {TempFileName,nq} 。我将如何实现这一目标?

最佳答案

您可以使用 conditional operator (?:)

[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}{IsTempFile ? \", TempFileName: \" + TempFileName : System.String.Empty,nq}")]

IsTempFile == false

enter image description here


IsTempFile == true

enter image description here

关于c# - 是否可以在 DebuggerDisplay 中使用条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892825/

相关文章:

c# - 极简 LINQ 方法 - System.NullReferenceException

c# - 在 Silverlight 中为 Bing map 设置最小/最大缩放

c# - 如何远程验证电子邮件或检查电子邮件是否存在

debugging - 进入程序运行时错误并打印出所有内容

function - Lua 全局覆盖

c# - 从 C# 与 Lotus Notes 交互的最佳方式和推荐做法是什么

javascript - 检测浏览器并且不加载HTML、cshtml cSharp

c# - 如何在 LINQ 查询中从字典获取值到新对象

c# - 如何创建 HttpPostedFileBase(或其继承类型)的实例

c - addr2line 返回 ?? :0 even when compiled with -g3, gdb 回溯有效