Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Json序列化支持字段引用属性,而不是反过来引用属性_C#_Json_Serialization_Json.net_Field - Fatal编程技术网

C#Json序列化支持字段引用属性,而不是反过来引用属性

C#Json序列化支持字段引用属性,而不是反过来引用属性,c#,json,serialization,json.net,field,C#,Json,Serialization,Json.net,Field,我使用NewtonSoft.Json最新版本,需要插入第三方对象。我需要序列化字段。以下是我使用的JsonSerializerSettings: JsonSerializerSettings settings_ = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, DefaultValueHandling = DefaultValueHandling.Include, P

我使用NewtonSoft.Json最新版本,需要插入第三方对象。我需要序列化字段。以下是我使用的JsonSerializerSettings:

JsonSerializerSettings settings_ = new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Include,
    DefaultValueHandling = DefaultValueHandling.Include,
    PreserveReferencesHandling = PreserveReferencesHandling.All,
    ObjectCreationHandling = ObjectCreationHandling.Replace,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
    ContractResolver = new DynamicContractResolver(),
};
public class DynamicContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
    {
        IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
        properties = properties.Where(p => p.PropertyName != "MonitoringInstance" && p.PropertyName != "SessionInstance").ToList();
        var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Select(f => CreateProperty(f, memberSerialization));
        return properties.Union(fields, new EqualityComparer()).ToList();
    }
}
JsonSerializerSettings设置\新的JsonSerializerSettings
{
NullValueHandling=NullValueHandling。包括,
DefaultValueHandling=DefaultValueHandling。包括,
PreserveReferencesHandling=PreserveReferencesHandling.All,
ObjectCreationHandling=ObjectCreationHandling.Replace,
ReferenceLoopHandling=ReferenceLoopHandling.Serialize,
ContractResolver=新的DynamicContractResolver(),
};
公共类DynamicContractResolver:DefaultContractResolver
{
受保护的重写IList CreateProperties(类型为Newtonsoft.Json.MemberSerialization MemberSerialization)
{
IList properties=base.CreateProperties(类型、成员序列化);
properties=properties.Where(p=>p.PropertyName!=“MonitoringInstance”&p.PropertyName!=“SessionInstance”).ToList();
var fields=type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
返回properties.Union(字段,new EqualityComparer()).ToList();
}
}

不幸的是,无论我如何尝试,backing字段都会引用该属性。有没有办法扭转这种局面?非常感谢您提供的任何见解。

只需使用
DataContractAttribute
DataMemberAttribute
字段来装饰您的类就容易多了。除非你真的想自己实现契约解析器,否则我们一直都在用JSON来实现它。

这是第三方对象。你知道你的第三方对象首先是可序列化的吗?根据对象的大小,将所述对象复制到可序列化的DTO中可能是有意义的。它具有[serializable]属性。它很大。因此,DTO的效果适得其反。相应的属性已正确序列化,但支持字段为null。