如何解决如何定位记录类的属性?
在定义记录类时,如何将属性定位到参数,字段或属性?
例如,我想使用JsonIgnore
,但这不能编译,因为它对字段或属性有属性使用限制:
record Person(string FirstName,string LastName,[JsonIgnore] int Age);
解决方法
要针对扩展类的各个部分,请使用适当的属性目标。例如:
// Target the property,use `property`
record Person(string FirstName,string LastName,[property: JsonIgnore] int Age);
// Target the backing field of the property,use `field`
record Person(string FirstName,[field: JsonIgnore] int Age);
// Target the constructor parameter,use `param`
record Person(string FirstName,[param: SomeParamAttribute] int Age);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。