gpt4 book ai didi

c# - 没有对象定义的二进制反序列化

转载 作者:太空狗 更新时间:2023-10-29 20:50:19 25 4
gpt4 key购买 nike

我正在尝试读取一个二进制序列化对象,但我没有它的对象定义/源。我浏览了该文件并看到了属性名称,因此我手动重新创建了该对象(我们称它为 SomeDataFormat)。

我最终得到了这个:

public class SomeDataFormat // 16 field
{
public string Name{ get; set; }
public int Country{ get; set; }
public string UserEmail{ get; set; }
public bool IsCaptchaDisplayed{ get; set; }
public bool IsForgotPasswordCaptchaDisplayed{ get; set; }
public bool IsSaveChecked{ get; set; }
public string SessionId{ get; set; }
public int SelectedLanguage{ get; set; }
public int SelectedUiCulture{ get; set; }
public int SecurityImageRefId{ get; set; }
public int LogOnId{ get; set; }
public bool BetaLogOn{ get; set; }
public int Amount{ get; set; }
public int CurrencyTo{ get; set; }
public int Delivery{ get; set; }
public bool displaySSN{ get; set; }
}

现在我可以像这样反序列化它了:

BinaryFormatter formatter = new BinaryFormatter();  
formatter.AssemblyFormat = FormatterAssemblyStyle.Full; // original uses this
formatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded; // this reduces size
FileStream readStream = new FileStream("data.dat", FileMode.Open);
SomeDataFormat data = (SomeDataFormat) formatter.Deserialize(readStream);

第一个可疑之处是只有 2 个字符串(SessionIdUserEmail)在反序列化的 data 对象中具有值。其他属性为 null 或仅为 0。这可能是有意为之,但我仍然怀疑在反序列化过程中出现了问题。

第二个可疑的事情是,如果我重新序列化这个对象,我最终会得到不同的文件大小。原始(695 字节)。重新序列化的对象是 698 字节。所以有3个字节的差异。我应该得到与原始文件大小相同的文件。

查看原始文件和新的(重新序列化的)文件:

原始序列化文件:(zoom) enter image description here重新序列化的文件:(zoom) enter image description here

如您所见,在 header 部分之后,数据似乎处于不同的顺序。例如,您可以看到电子邮件和 sessionID 不在同一个地方。

更新:将警告我“PublicKeyToken=null”之后的字节也不同。 (03 <-> 05)

  • Q1:为什么两个文件中的值顺序不同?
  • Q2:为什么2个序列化后的对象多了3个字节?
  • Q3:我错过了什么?我该怎么做?

感谢任何帮助。


相关问题的种类: 1 2 3

最佳答案

Why are the values are in different order in the two files?

那是因为成员顺序不是基于声明顺序。 http://msdn.microsoft.com/en-us/library/424c79hc.aspx

The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which members are returned, because that order varies.

.

Why is there extra 3 bytes compared the 2 serialized objects?

首先,TypeFormat“TypesWhenNeeded”实际上应该是“TypesAlways”。这就是为什么会有如此多的差异。例如,'=null' 之后的 05 变为 03 就是由于此。

其次,您没有正确的类型。查看 ILSpy 中的 BinaryFormatter 和十六进制转储显示您标记为“int”的成员实际上是“string”。

public class SomeDataFormat // 16 field
{
public string Name { get; set; }
public string Country { get; set; }
public string UserEmail{ get; set; }
public bool IsCaptchaDisplayed{ get; set; }
public bool IsForgotPasswordCaptchaDisplayed{ get; set; }
public bool IsSaveChecked{ get; set; }
public string SessionId{ get; set; }
public string SelectedLanguage{ get; set; }
public string SelectedUiCulture{ get; set; }
public string SecurityImageRefId{ get; set; }
public string LogOnId{ get; set; }
public bool BetaLogOn{ get; set; }
public string Amount{ get; set; }
public string CurrencyTo{ get; set; }
public string Delivery{ get; set; }
public bool displaySSN{ get; set; }
}

What am I missing? How could I do this?

我看不到使用给定的 BinaryFormatter 来完成此操作的方法。您可以反编译/反转 BinaryFormatter 的工作方式。

关于c# - 没有对象定义的二进制反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com