除了使用 [JsonIgnore] 装饰器之外,还有其他方法可以在 JSON 序列化期间忽略属性吗?

Sai*_*ngi 4 c# data-binding json-serialization maui community-toolkit-mvvm

我正在使用 .net Maui MVVM 源生成器来创建属性作为可观察属性。我正在使用 System.Text.Json.Serialization 将类的属性序列化为 JSON。当我在某些属性上使用 [JsonIgnore] 时,它们仍然会序列化为 JSON。还有其他方法可以忽略属性吗?

我认为问题是我将装饰器放在私有属性声明上,而不是公共属性声明上,因为公共属性是在依赖项 -> 分析器 -> CommunityToolkit.Mvvm.SourceGenerators 中创建的。

在此输入图像描述

Too*_*eve 5

我似乎找不到博客文章,但将属性传播到生成的属性是这样的语法[property: SomePropertyAttributeHere]::

using CommunityToolkit.Mvvm.ComponentModel;
using System.Text.Json.Serialization;
...
[ObservableProperty]
[property: JsonIgnore]
RecommendationsType recommendationsList;
Run Code Online (Sandbox Code Playgroud)

这会生成一个带有附加属性的属性:

[JsonIgnore]
RecommendationsType RecommendationsList
{
    get ...
    set ...
}
Run Code Online (Sandbox Code Playgroud)