C#9 不支持 JsonPropertyNameAttribute 记录? [英] JsonPropertyNameAttribute is not supported record from C#9?

查看:32
本文介绍了C#9 不支持 JsonPropertyNameAttribute 记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有 JsonPropertyName 属性的记录,但它导致了错误.这个不支持?有什么解决方法吗?

I want to use record with JsonPropertyName attribute, but it caused an error. This is not supported? Any workaround?

public record QuoteResponse([JsonPropertyName("quotes")] IReadOnlyCollection<Quote>? Quotes);

Error CS0592 Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer, field' declarations.

推荐答案

默认情况下,记录参数的属性适用于参数.要使它们适用于属性,您必须在它前面加上 property: 属性 location:

By default attributes on record parameters apply to the parameter. To make them apply to the property you have to prefix it with the property: attribute location:

public record QuoteResponse([property: JsonPropertyName("quotes")] IReadOnlyCollection<Quote>? Quotes);

这篇关于C#9 不支持 JsonPropertyNameAttribute 记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆