[英]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:如果我在
[DebuggerDisplay]
上指定(日期)格式,则会看到错误 CS0726:
error CS0726: ':d' is not a valid format specifier
错误 CS0726:“:d”不是有效的格式说明符
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:在 Visual Studio 中调试时显示:
For specifying the format on the [DebuggerDisplay]
you need an expression, eg ToString("d")
- and escape the quotes.要在
[DebuggerDisplay]
上指定格式,您需要一个表达式,例如ToString("d")
- 并转义引号。
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.我还添加了一个
,nq
所以我们不会渲染额外的引号。
See Using Expressions in DebuggerDisplay请参阅在 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注意:
,d
不适用于指定格式 - 它不会给出错误但我也不会更改格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.