为一个值制作 属性 [NonSerialized]
Making property [NonSerialized] for a value
我想弄清楚如何为一个值
制作一个属性[NonSerialized]
看看这个:
using System;
using System.Text.Json;
class Test
{
public static bool GoingToBeSerialized = false;
public int PaymentForTheDay { get; set; }
public int NumberOfDays { get; set; }
// i want to disable it if GoingToBeSerialized is true
[System.Text.Json.Serialization.JsonIgnore]
public int TotalPayment;
public bool ShouldSerializeTotalPayment() => GoingToBeSerialized;
}
谢谢。
请注意,[Serializable]
和 [NonSerialized]
(在原始问题中,现在已在编辑中删除)对大多数序列化程序 什么都不做 - 它们仅适用于BinaryFormatter
,您没有使用。
很有可能只需使用:
public int TotalPayment {get;set;}
public bool ShouldSerializeTotalPayment() => GoingToBeSerialized;
会为所欲为;最近添加了显示您正在使用 Json.NET 的 pastebin,这应该确实有效 - conditional serialization is a Json.NET feature using the standard pattern。另请注意,我将 TotalPayment
设为 属性,并删除了 [JsonIgnore]
.
我想弄清楚如何为一个值
制作一个属性[NonSerialized]看看这个:
using System;
using System.Text.Json;
class Test
{
public static bool GoingToBeSerialized = false;
public int PaymentForTheDay { get; set; }
public int NumberOfDays { get; set; }
// i want to disable it if GoingToBeSerialized is true
[System.Text.Json.Serialization.JsonIgnore]
public int TotalPayment;
public bool ShouldSerializeTotalPayment() => GoingToBeSerialized;
}
谢谢。
请注意,[Serializable]
和 [NonSerialized]
(在原始问题中,现在已在编辑中删除)对大多数序列化程序 什么都不做 - 它们仅适用于BinaryFormatter
,您没有使用。
很有可能只需使用:
public int TotalPayment {get;set;}
public bool ShouldSerializeTotalPayment() => GoingToBeSerialized;
会为所欲为;最近添加了显示您正在使用 Json.NET 的 pastebin,这应该确实有效 - conditional serialization is a Json.NET feature using the standard pattern。另请注意,我将 TotalPayment
设为 属性,并删除了 [JsonIgnore]
.