C# 使用 Serializable 属性和实现 ISerializable 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2365152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:46:13  来源:igfitidea点击:

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

c#inheritanceattributesserializationiserializable

提问by SoftwareGeek

What's the difference between using the Serializableattribute and implementing the ISerializableinterface?

使用Serializable属性和实现ISerializable接口有什么区别?

采纳答案by t0mm13b

When you use the SerializableAttributeattribute 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.

当您使用SerializableAttribute属性时,您是在编译时将属性放在字段上,这样在运行时,序列化工具将通过对类/模块/程序集执行反射来知道要根据属性进行序列化的内容类型。

[Serializable]
public class MyFoo { … }

The above indicates that the serializing facility should serialize the entire class MyFoo, whereas:

以上表明序列化工具应该序列化整个 class 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.

使用该属性,您可以有选择地选择需要序列化的字段。

When you implement the ISerializableinterface, the serialization effectively gets overridden with a custom version, by overriding GetObjectDataand 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.

当您实现ISerializableinterface 时,序列化会被自定义版本有效地覆盖,通过覆盖(并通过提供表单的构造函数),可以更好地控制数据的序列化。GetObjectDataSetObjectDataMyFoo(SerializationInfo info, StreamingContext context)

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.

另请参阅StackOverflow 上的自定义序列化示例。它展示了如何保持序列化与序列化数据的不同版本向后兼容。

Hope this helps.

希望这可以帮助。

回答by Segfault

The SerializableAttributeinstructs the framework to do the default serialization process. If you need more control, you can implement the ISerializable interface. Then you would put the your own code to serialize the object in the GetObjectDatamethod and update the SerializationInfoobject that is passed in to it.

SerializableAttribute指示做框架默认序列化过程。如果需要更多控制,可以实现ISerializable 接口。然后,您可以将自己的代码用于序列化GetObjectData方法中的SerializationInfo对象并更新传递给它的对象。

回答by Andrey

ISerialize force you to implement serialization logic manially, while marking by Serializable attribute (did you mean it?) will tell Binary serializer that this class can be serialized. It will do it automatically.

ISerialize 强制您手动实现序列化逻辑,而通过 Serializable 属性标记(您是不是这个意思?)会告诉 Binary serializer 这个类可以序列化。它会自动完成。

回答by logicnp

Inheriting from ISerializable allows you to custom implement the (de)serialization. When using only the Serializable attribute, the (de)serialization can be controlled only by attributes and is less flexible.

从 ISerializable 继承允许您自定义实现(反)序列化。仅使用 Serializable 属性时,(反)序列化只能由属性控制,灵活性较差。

回答by Asad

The ISerializableinterface lets you implement custom serializationother than default. When you implement the ISerializableinterface, you have to override GetObjectDatamethod as follows

ISerializable接口允许您实现除默认之外的自定义序列化。实现ISerializable接口时,必须重写GetObjectData方法如下

public void GetObjectData (SerializationInfo serInfo, 
                                    StreamingContext streamContext)
{
   // Implement custom Serialization
}