C#9中的记录不支持JsonPropertyNameAttribute?
我想使用带有JsonPropertyName属性的记录,但它导致了一个错误。这是不受支持的吗?有解决办法吗C#9中的记录不支持JsonPropertyNameAttribute?,c#,.net-5,system.text.json,c#-9.0,C#,.net 5,System.text.json,C# 9.0,我想使用带有JsonPropertyName属性的记录,但它导致了一个错误。这是不受支持的吗?有解决办法吗 public record QuoteResponse([JsonPropertyName("quotes")] IReadOnlyCollection<Quote>? Quotes); Error CS0592 Attribute 'JsonPropertyName' is not valid on this declaration type. It i
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.
public record quoterresponse([JsonPropertyName(“quotes”)]IReadOnlyCollection?quotes);
错误CS0592属性“JsonPropertyName”对此声明类型无效。它仅对“属性、索引器、字段”声明有效。
创建记录有两种方法(我知道),下面的方法有望为您解决
public record QuoteResponse
{
[JsonPropertyName("quotes")]
public IReadOnlyCollection<Quote>? Quotes {get; init;}
}
公共记录报价响应
{
[JsonPropertyName(“报价”)]
公共IReadOnlyCollection?引号{get;init;}
}
默认情况下,记录参数上的属性应用于参数。要使它们应用于属性,必须在属性前面加上属性:
属性位置:
public record quoterresponse([property:JsonPropertyName(“quotes”)]IReadOnlyCollection?quotes);
如果没有Set或Init修饰符,属性将不会初始化。那么它与类有什么不同呢?感谢您注意到它应该在get之后有一个init。我现在将更新我的代码。密切相关的问题