我想知道是否可以让调试器显示 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/