我们通过(de)序列化一个名为“DocumentClass”的类来保存和读取文件。 一切正常,直到我们添加了2个字段到文档类。 (我们认为这是问题)序列化问题
当我们现在尝试打开由以前版本序列化的文件时,我们得到一个错误。
System.ArgumentException:System.Int32类型的对象无法转换为类型'System.String'。 在SoftwareProject.Componenten.Bestand.DocumentClass.d(字符串A_0) 在de..ctor(字符串A_0) 在G.A(字符串A_0)
的方法,产生所述误差是方法 “读取”。 (DocumentClass.d()是混淆的名称)
但事情变得更加怪异:当我们在VS调试模式下打开文件时,没有错误产生,但文档类中的所有字段都是0或空?
我们在这里输了...请帮助... 我们已经添加了[OptionalField]属性到新的领域,但这并不能帮助..
为什么在调试空的所有值模式?? 运行时错误来自哪里?我们如何调试它?
在此先感谢!
public static DocumentClass Read(string fullFilePath)
{
DocumentClass c = new DocumentClass();
Stream s = File.OpenRead(fullFilePath);
BinaryFormatter b = new BinaryFormatter();
//b.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
b.Binder = new MyCustomBinder();
try
{
c = (DocumentClass)b.Deserialize(s);
}
catch(Exception exc)
{
s.Close();
throw exc;
}
finally
{
s.Close();
}
return c;
}
public class MyCustomBinder : SerializationBinder {
public override Type BindToType(string assemblyName, string typeName) {
Type tyType = null;
string sShortAssemblyName = assemblyName.Split(',')[0];
Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
if (sShortAssemblyName.ToLower() == "debugAssemblyName")
{
sShortAssemblyName = "AppAssemblyName";
}
foreach (Assembly ayAssembly in ayAssemblies) {
if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) {
tyType = ayAssembly.GetType(typeName);
break;
}
}
return tyType;
}
}