我正在使用 .net Maui MVVM 源生成器将属性创建为可观察属性。我正在使用 System.Text.Json.Serialization 将类的属性序列化为 JSON。当我在某些属性上使用 [JsonIgnore] 时,它们仍然会序列化为 JSON。还有其他忽略属性的方法吗?
我认为问题是我将装饰器放在私有属性声明上,而不是公共属性声明上,因为公共属性是在依赖项 -> 分析器 -> CommunityToolkit.Mvvm.SourceGenerators 中创建的。
我似乎找不到博客文章,但将属性传播到生成的属性是这样的语法:
[property: SomePropertyAttributeHere]
:
using CommunityToolkit.Mvvm.ComponentModel;
using System.Text.Json.Serialization;
...
[ObservableProperty]
[property: JsonIgnore]
RecommendationsType recommendationsList;
这会生成一个带有附加属性的属性:
[JsonIgnore]
RecommendationsType RecommendationsList
{
get ...
set ...
}