c# - Visual Studio: [DebuggerDisplay], shows "not a valid format specifier" when specifying format
问题描述
If I specify a (date) format on the [DebuggerDisplay]
, I see a error CS0726:
error CS0726: ':d' is not a valid format specifier
For example this code:
[DebuggerDisplay("{From:d} - {To:d}")
public class DateRange
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
Shows when debugging in Visual Studio:
解决方案
For specifying the format on the [DebuggerDisplay]
you need an expression, e.g. ToString("d")
- and escape the quotes.
This works:
[DebuggerDisplay("{From.ToString(\"d\"),nq} - {To.ToString(\"d\"),nq}")
public class DateRange
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
I also added a ,nq
so we don't render extra quotes.
See Using Expressions in DebuggerDisplay
Result:
Note: ,d
won't work for specifying the format - It won't give an error but I also won't change the format
推荐阅读
- c# - C# Linq 如何使用对象的数据过滤列表并将结果存储在另一个列表中?
- python - Pygame 碰撞没有完美发生
- c++ - 如果 Unreal Engine 5 c++ 中的整数值大于 127,则 Protoc 无法解析输入
- python - 如何在 Matplotlib 中显示顶部和底部 5 行值以及自定义行值?
- javascript - Vue - 添加背景动画
- android - youtube_player_flutter 颤振播放速度
- python - Python中比较布尔值的时间复杂度和运行速度
- concurrency - F# 平行角色
- javascript - Chrome有时将元素作为节点有时作为标记
- clr - 如何在此 winim 库中使用 ProcessStartInfo 实例化应用程序