使用 BinaryFormatter 对引用进行二进制序列化

发布于 2024-10-31 05:35:22 字数 544 浏览 5 评论 0原文

给定以下类结构,Bar 会按预期进行序列化/反序列化吗?

public class Foo { int x; string y; }

[Serializable]
public class Bar {
   Foo[] AllFoos;
   Foo SelectedFoo;

   public Bar(Foo[] allFoos, int selectedFooIndex) { 
     this.AllFoos = allFoos; 
     this.SelectedFoo = allFoos[selectedFooIndex]; 
   } 
}

我对以下几件事感到好奇:

1)BinaryFormatter 是否要求 Bar 类用 [Serialized] 属性修饰或实现 ISerialized 接口?

2)Foo类是否也需要用[Serialized]属性来修饰?

3)如果Bar简单地用[Serialized]属性修饰,字段Bar.SelectedFoo是否会正确维护其对数组的引用?或者我最终会得到那个 Foo 的副本吗?

Given the following class structure, will Bar serialize/deserialize as expected?

public class Foo { int x; string y; }

[Serializable]
public class Bar {
   Foo[] AllFoos;
   Foo SelectedFoo;

   public Bar(Foo[] allFoos, int selectedFooIndex) { 
     this.AllFoos = allFoos; 
     this.SelectedFoo = allFoos[selectedFooIndex]; 
   } 
}

I'm curious about a couple of things:

1) Does the BinaryFormatter REQUIRE that the Bar class be decorated with the [Serializable] attribute or implement the ISerializable interface?

2) Does the Foo class also need to be decorated with the [Serializable] attribute?

3) If Bar is simply decorated with the [Serializable] attribute, will the field Bar.SelectedFoo maintain its reference into the array correctly? or will I wind up with a copy of that Foo?

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

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

发布评论

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

评论(1

爱格式化 2024-11-07 05:35:22

1) BinaryFormatter 是否要求 Bar 类用 [Serialized] 属性修饰或实现 ISerialized 接口?

是的,如果要使用 BinaryFormatter 序列化 Bar 实例,则确实如此。

2)Foo类是否也需要用[Serialized]属性修饰?

是的,除非您创建不涉及序列化 Foo 对象实例的自定义序列化机制。例如,您可以分别序列化 x 和 y 组件,并在反序列化代码中根据它们创建一个新的 Foo 实例。否则,它必须具有属性或接口。

3) 如果 Bar 只是用 [Serialized] 属性修饰,字段 Bar.SelectedFoo 是否会正确维护其对数组的引用?或者我最终会得到那个 Foo 的副本吗?

如果我没记错的话,数组不能像这样序列化。您必须提供自己的机制(通过 ISerialized 接口)来写入和读取数组。

但是,一般来说,如果使用 BinaryFormatter 序列化具有相互引用的可序列化对象图,那么它将正确重新创建引用,而不会重复对象。这也应该包括您在自定义序列化代码中指定的对象,只要您使用 Serialized 修饰 Foo 并将相同的对象实例从数组和字段传递给格式化程序即可。

1) Does the BinaryFormatter REQUIRE that the Bar class be decorated withthe [Serializable] attribute or implement the ISerializable interface?

Yes, it does, if the BinaryFormatter is to be used to serialize a Bar instance.

2) Does the Foo class also need to be decorated with the [Serializable] attribute?

Yes, unless you create a custom serialization mechanism that does not involve serializing an instance of a Foo object. For example, you could serialize the x and y components separately, and create a new Foo instance from them in your deserialization code. Otherwise, it must have the attribute or interface.

3) If Bar is simply decorated with the [Serializable] attribute, will the field Bar.SelectedFoo maintain its reference into the array correctly? or will I wind up with a copy of that Foo?

If I remember correctly, arrays are not serializable like this. You must provide your own mechanism (through the ISerializable inteface) for writing and reading arrays.

However, in general, if a graph of serializable objects with mutual references to each other is serialized with the BinaryFormatter, then it will recreate the references correctly without duplicating objects. This should include objects that you specify in your custom serialization code as well, so long as you decorate your Foo with Serializable and pass the same object instance to the formatter from both the array and the field.

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