使用BinaryFormatter反序列化字典时偶现‘Version成员未找到’错误排查
存档加载错误排查:Member 'Version' was not found
错误堆栈信息
Member 'Version' was not found. at System.Runtime.Serialization.SerializationInfo.GetElement (System.String name, System.Type& foundType) [0x00000] in <00000000000000000000000000000000>:0 at System.Runtime.Serialization.SerializationInfo.GetInt32 (System.String name) [0x00000] in <00000000000000000000000000000000>:0 at System.Collections.Generic.Dictionary`2[TKey,TValue].OnDeserialization (System.Object sender) [0x00000] in <00000000000000000000000000000000>:0 at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize (System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Runtime.Serialization.Formatters.Binary.__BinaryParser serParser, System.Boolean fCheck) [0x00000] in <00000000000000000000000000000000>:0 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <00000000000000000000000000000000>:0
问题现象
- 开发的Steam平台Unity游戏,采用
BinaryFormatter序列化存档并通过Steam云存储存取,极少数用户出现存档加载失败 - 用户退出游戏并重启若干次后,存档可正常加载
- 已排除空文件/乱码文件的情况:反序列化空文件或乱码文件仅触发警告,不会抛出该错误
潜在代码违规点(违反CA2218规则)
自定义SimpleVector3对象的序列化相关代码存在规则违规:
public override int GetHashCode() { return new Vector3(x, y, z).GetHashCode(); } public override bool Equals(object obj) { if (obj is SimpleVector3 sv3) { return sv3.x == x && sv3.y == y && sv3.z == z; } return false; }
问题在于:Equals()直接比较浮点值(存在浮点误差风险),而GetHashCode()通过转换为Unity的Vector3生成哈希码,可能出现哈希码相同但Equals()返回false的情况。
核心疑问与排查需求
- 浮点误差导致的序列化/反序列化行为不一致,是否会引发上述特定的"Member 'Version' was not found"错误?
- 基于该错误信息,有哪些具体的排查方向线索?
内容的提问来源于stack exchange,提问作者John Gamble
相关产品推荐
相关产品推荐

