【发布时间】:2026-02-10 08:05:02
【问题描述】:
鉴于以下类结构,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 类用 [Serializable] 属性修饰或实现 ISerializable 接口?
2) Foo 类是否也需要用 [Serializable] 属性修饰?
3) 如果 Bar 简单地用 [Serializable] 属性修饰,字段 Bar.SelectedFoo 是否会正确地保持其对数组的引用?还是我会得到一份 Foo 的副本?
【问题讨论】:
标签: .net serialization reference binaryformatter