有没有办法将仅特定的C#属性标记为可序列化? [英] Is there a way to flag only specific C# properties as serializable?

查看:253
本文介绍了有没有办法将仅特定的C#属性标记为可序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[JsonIgnore]将阻止某些C#模型属性包含在JSON序列化中.我试图使模型上的所有属性都被忽略,除非它们具有[JsonInclude]属性,所以它更多是选择加入"而不是选择退出"方案.

[JsonIgnore] will prevent a certain C# model property from being included in the JSON serialization. I'm attempting to have ALL properties on a model ignored UNLESS they have a [JsonInclude] property, so it's more of an "opt-in" rather than "opt-out" scenario.

有什么办法可以做到这一点?

Is there any way to achieve this?

推荐答案

将以下属性应用于模型:

Apply the following attribute to the model:

[JsonObject(MemberSerialization.OptIn)]

然后修饰要包含在JsonProperty属性中的每个属性.

Then decorate each property you want included with the JsonProperty attribute.

JsonObjectAttribute

这篇关于有没有办法将仅特定的C#属性标记为可序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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