使用可序列化属性和使用可序列化属性有什么区别?实现ISerialized?

发布于 2024-08-23 16:30:12 字数 73 浏览 6 评论 0原文

使用 Serialized 属性和实现 ISerialized 接口有什么区别?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原谅我要高飞 2024-08-30 16:30:12

当您使用 SerializedAttribute属性,您在编译时将属性放在字段上,这样在运行时,序列化工具将通过对类/模块执行反射来知道根据属性序列化什么内容/装配类型。

[Serializable]
public class MyFoo { … }

上面表明序列化工具应该序列化整个类 MyFoo,而:

public class MyFoo
{
    private int bar;

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

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

当您实现 ISerialized 接口,通过覆盖GetObjectData SetObjectData (以及通过提供 MyFoo(SerializationInfo info, StreamingContext context)) 形式的构造函数,可以更好地控制数据的序列化。

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

希望这有帮助。

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 { … }

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

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 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.

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.

Hope this helps.

蹲在坟头点根烟 2024-08-30 16:30:12

SerializedAttribute 指示框架执行默认的序列化过程。如果您需要更多控制,可以实现 ISerialized界面。然后,您可以在 GetObjectData 方法中放置您自己的代码来序列化该对象,并更新传递给它的 SerializationInfo 对象。

The SerializableAttribute instructs 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 GetObjectData method and update the SerializationInfo object that is passed in to it.

绅刃 2024-08-30 16:30:12

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

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

The ISerializable interface lets you implement custom serialization other than default.
When you implement the ISerializable interface, you have to override GetObjectData method as follows

public void GetObjectData (SerializationInfo serInfo, 
                                    StreamingContext streamContext)
{
   // Implement custom Serialization
}
不知在何时 2024-08-30 16:30:12

ISerialize 强制您手动实现序列化逻辑,而通过 Serialized 属性标记(您是认真的吗?)将告诉 Binary 序列化程序该类可以序列化。它会自动完成。

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

沙沙粒小 2024-08-30 16:30:12

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文