您的位置:首页 > 其它

数据契约事件

2012-04-17 17:19 323 查看
.NET数据契约DataContract提供4个事件

[OnDeserialized|OnDeserializing|OnSerialized|OnSerializing]

我们可以通过以上事件在序列化或反序列化时实现构造函数的功能

[DataContract]
public class TestInfo
{
[DataMember(Name = "t1")]
public int Test1{ get; set; }

[DataMember(Name = "t2")]
public string Test2{ get; set; }

[OnDeserialized]
void OnDeserialized(StreamingContext context)
{
if (this.Test2 == null)
this.Test2 = "deserialized";
}

[OnSerializing]
void OnSerializing(StreamingContext context)
{
if (this.Test2 == null)
this.Test2 = "serializing";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: