什么是使用Serializable属性与放大器之间的差异;实现了ISerializable? [英] What's the difference between using the Serializable attribute & implementing ISerializable?

查看:146
本文介绍了什么是使用Serializable属性与放大器之间的差异;实现了ISerializable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么用序列化属性贯彻 ISerializable的接口的区别?

What's the difference between using the Serializable attribute and implementing the ISerializable interface?

推荐答案

当您使用<$c$c>SerializableAttribute属性你以这样的方式,当在运行时,串行化设施将知道根据通过在类/模块/组件类型进行反​​射的属性来序列什么把一个属性上的场在编译时

When you use the SerializableAttribute attribute you are putting an attribute on a field at compile-time in such a way that when at run-time, the serializing facilities will know what to serialize based on the attributes by performing reflection on the class/module/assembly type.

[Serializable]
public class MyFoo { … }

以上表明该序列化设施应序列全班 MyFoo ,而

public class MyFoo
{
    private int bar;

    [Serializable]
    public int WhatBar
    {
       get { return this.bar; }
    }
}

使用可以有选择性地选择哪些字段需要被序列化的属性。

Using the attribute you can selectively choose which fields needs to be serialized.

在实施<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx\"><$c$c>ISerializable接口,系列化有效地得到一个定制版本覆盖,通过重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx\"><$c$c>GetObjectData <击>和 SetObjectData (并提供以下形式的构造函数 MyFoo(的SerializationInfo信息,的StreamingContext上下文)),将有超过数据的序列化更好的控制程度。

When you implement the ISerializable interface, the serialization effectively gets overridden with a custom version, by overriding GetObjectData and SetObjectData (and by providing a constructor of the form MyFoo(SerializationInfo info, StreamingContext context)), there would be a finer degree of control over the serializing of the data.

又见<一个href=\"http://stackoverflow.com/questions/2121424/serializing-data-transfer-objects-in-net/2121517#2121517\">this例如这里自定义序列在计算器上的。它显示了如何保持与序列化序列化数据的不同versionings向后兼容。

See also this example of a custom serialization here on StackOverflow. It shows how to keep the serialization backwards-compatible with different versionings of the serialized data.

希望这有助于。

这篇关于什么是使用Serializable属性与放大器之间的差异;实现了ISerializable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆