什么是使用Serializable属性与放大器之间的差异;实现了ISerializable? [英] What's the difference between using the Serializable attribute & implementing ISerializable?
问题描述
有什么用序列化
属性贯彻 ISerializable的
接口的区别?
What's the difference between using the Serializable
attribute and implementing the ISerializable
interface?
推荐答案
当您使用<$c$c>SerializableAttribute$c$c>属性你以这样的方式,当在运行时,串行化设施将知道根据通过在类/模块/组件类型进行反射的属性来序列什么把一个属性上的场在编译时
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$c$c>接口,系列化有效地得到一个定制版本覆盖,通过重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.getobjectdata.aspx\"><$c$c>GetObjectData$c$c> <击>和 SetObjectData code> 罢工>(并提供以下形式的构造函数
MyFoo(的SerializationInfo信息,的StreamingContext上下文)
),将有超过数据的序列化更好的控制程度。
When you implement the ISerializable
interface, the serialization effectively gets overridden with a custom version, by overriding GetObjectData
and (and by providing a constructor of the form SetObjectData
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屋!