c# - 在 PropertyGrid C# 中显示 DebuggerDisplay

标签 c# properties propertygrid debuggerdisplay

我想知道是否可以让调试器显示 PropertyGrid 中类的文本?

我似乎无法在任何地方找到这个答案。

这是我所拥有的示例。

[DebuggerDisplay("FPS = {FPS}")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class DebugModule : Module
{
     public int FPS {get; set;}
}

此模块包含在引擎类中,因此当我设置 propertyGrid.SelectedObject = engineInstance 时,我希望在属性网格中看到

Engine

+ DebugModuel | "FPS = 60"

FPS | 60

最佳答案

这个怎么样,它在调试器和 PropertyGrid 中显示相同的文本:

[DebuggerDisplay("{.}")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class DebugModule : Module
{
    public int FPS { get; set; }

    public override string ToString() { return "FPS = " + FPS; }
}

或者如果您需要将 ToString 用于其他用途:

[DebuggerDisplay("{DebugDisplayText}")]
[TypeConverter(typeof(DebugModuleConverter))]
public class DebugModule : Module
{
    public int FPS { get; set; }

    private string DebugDisplayText { get { return "FPS = " + FPS; } }

    public class DebugModuleConverter : ExpandableObjectConverter {
        public override object ConvertTo(ITypeDescriptorContext context,
                System.Globalization.CultureInfo culture, object value,
                Type destinationType) {
            if(destinationType == typeof(string)) {
                return ((DebugModule) value).DebugDisplayText;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

关于c# - 在 PropertyGrid C# 中显示 DebuggerDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3288304/

相关文章:

java - 使用 Spring Boot 将 YAML 属性绑定(bind)到 Map<String, List<String>> 类型

tomcat - 如何在启动时在 Tomcat 配置中指定系统属性?

c# - 拥有私有(private) setter 或仅定义 getter 之间有区别吗?

c# - 从 PropertyGrid 显示详细的文件夹浏览器

c# - 更改 PropertyGrid 左侧集合编辑器/ View 的宽度

c# - 从 SqlDataReader 创建 JSON 字符串

c# - LinqtoCsv 写入 - 创建新的或追加

c# - 如何在 PropertyGrid 中查看对象属性?

c# - 如何在 WPF 的 Combobox 中有效地添加多个项目

c# - 无法等待进程运行直到结束