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自动序列化标记为[Ignore]的属性_C#_Json_Serialization - Fatal编程技术网

C# 是否可以用JSON自动序列化标记为[Ignore]的属性

C# 是否可以用JSON自动序列化标记为[Ignore]的属性,c#,json,serialization,C#,Json,Serialization,我有一个(简化的)对象,如下所示: public class Contact { [Ignore] public string Name { get; set;} } 我序列化并反序列化该对象以创建深度副本克隆(),如下所示: var反序列化设置=新的JsonSerializerSettings { ObjectCreationHandling=ObjectCreationHandling.Replace, }; var clone=JsonConvert.Serialize

我有一个(简化的)对象,如下所示:

public class Contact 
{
    [Ignore]
    public string Name { get; set;}
}
我序列化并反序列化该对象以创建深度副本克隆(),如下所示:

var反序列化设置=新的JsonSerializerSettings
{
ObjectCreationHandling=ObjectCreationHandling.Replace,
};
var clone=JsonConvert.SerializeObject(联系人,反序列化设置);
返回JsonConvert.DeserializeObject(克隆);
但该属性不会被复制


是否可以序列化标记为
[忽略]
的属性。可能使用
JsonSerializerSettings
,或者我需要将其标记为
[JSONProperty]

我想一种方法是将忽略的字段添加回去:

JsonConvert.DeserializeObject<Contact>(clone).Name=contact.Name


是属性真的忽略了还是jsonignore或ignoredatamemeberI使用了
DynamicContractResolver
,并且工作得很好。谢谢超出内部测试为什么在
ContractResolver
中有注释掉的行?你可以删除它们,这些只是你可以做的例子。
JsonConvert.DeserializeObject<Contact>(clone).Name=contact.Name
 var deserializeSettings = new JsonSerializerSettings
            {
                ObjectCreationHandling = ObjectCreationHandling.Replace,
                ContractResolver = new DynamicContractResolver()
            };
public class DynamicContractResolver: DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);

        //property.HasMemberAttribute = true;
        property.Ignored = false;

        //property.ShouldSerialize = instance =>
        //{
        //    return true;
        //};

        return property;
    }
}