我知道ScriptIgnoreAttribute
。
但是,如果我想忽略基于标准的属性怎么办。例如,仅当它为空且不包含任何值时,如何在序列化时忽略可空属性?
我知道ScriptIgnoreAttribute
。
但是,如果我想忽略基于标准的属性怎么办。例如,仅当它为空且不包含任何值时,如何在序列化时忽略可空属性?
https://docs.microsoft.com/dotnet/api/system.web.script.serialization.scriptignoreattribute
使用[ScriptIgnore]
using System;
using System.Web.Script.Serialization;
public class Group
{
// The JavaScriptSerializer ignores this field.
[ScriptIgnore]
public string Comment;
// The JavaScriptSerializer serializes this field.
public string GroupName;
}
我最好的答案是制作你自己的JavaScriptConverter并根据你自己的条件解析属性。
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
//...
if (!object.ReferenceEquals(dictionary["MyProperty"],null)){
// My Code
}
//...
}
我使用内部而不是公共属性,它对我有用