Setting the initial value of a property when using DataContractSerializer

不问归期 提交于 2019-12-01 15:36:27

You can use a serialization callback. Add the following method to your Person class:

[OnDeserialized]
void OnDeserialized(StreamingContext context)
{
    this.IsNew = true;
}

Another option is to remove the [DataContract] and [DataMember] attributes. In this case DCSerializer will call your constructor when it deserializes.

Actually the correct way of doing it is by using the OnDeserializing attribute (notice the "ing" suffix). The method marked with this attribute is called before the member values are deserialized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!