【发布时间】:2020-07-13 13:17:37
【问题描述】:
我正在尝试使用默认值属性Default Value Documentation
所以我知道如何用字符串做一个默认属性
[DefaultValue("This is the message")]
但是如何为字典设置默认值呢?
我试过了
[DefaultValue(new Dictionary<string, string>() { { "Test", "test" } })]
但我收到此错误
错误 CS0182 属性参数必须是常量表达式, 属性的 typeof 表达式或数组创建表达式 参数类型
【问题讨论】:
-
你不能。属性参数必须是 const 表达式,而构造对象则不是。如果您必须使用
DefaultValueAttribute,则无法完成您想要的。 -
我很惊讶there is no good 这个错误的规范答案。对于您需要以字典为值的
DefaultValueAttribute什么?序列化?某些控制(winforms?wpf?)?还有什么? -
我想给swagger.io一个默认值
标签: c# attributes