我知道ScriptIgnoreAttribute的事。
但是,如果我想忽略基于条件的属性怎么办?例如,如何在序列化时忽略仅当它为null且不包含任何值时才忽略nullable属性?
发布于 2011-05-14 03:41:17
我得到的最好的答案是制作您自己的JavaScriptConverter,并根据您自己的条件解析属性。
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
//...
if (!object.ReferenceEquals(dictionary["MyProperty"],null)){
// My Code
}
//...
}发布于 2015-09-06 01:30:29
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;
}发布于 2015-05-14 02:41:07
我使用内部属性而不是公共属性,这对我很有效
https://stackoverflow.com/questions/5996841
复制相似问题