【问题标题】:Deserialization in debug mode?调试模式下的反序列化?
【发布时间】:2010-11-28 11:49:39
【问题描述】:

谁能解释为什么会发生以下情况:

当我们在调试模式下序列化文件时,我们可以在调试模式下再次打开它,但不能在运行时打开。 当我们在运行时模式下序列化文件时,我们可以在运行时模式下再次打开它,但不能在调试模式下。

现在我知道你会说:那是因为它们有不同的程序集。 但是我们使用自定义的 Binder,如下所示... 此外,如果我们比较两种类型,“bool same = (o.GetType() == c.GetType())”,我们得到的结果总是“true”???

那为什么我们不能打开文件??

public class Binder : 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() == "debugName")
        {
            sShortAssemblyName = "runtimeName";
        }
        foreach (Assembly ayAssembly in ayAssemblies) {
            if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) {
                tyType = ayAssembly.GetType(typeName);
                break;
            }
        }
        return tyType;
    }
}



    public static DocumentClass Read(string fullFilePath, bool useSimpleFormat)
    {
        DocumentClass c = new DocumentClass();
        c.CreatedFromReadFile = true;

        Stream s = File.OpenRead(fullFilePath);// f.Open(FileMode.Open);
        BinaryFormatter b = new BinaryFormatter();
        if (useSimpleFormat)
        {
            b.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
        }
        b.Binder = new Binder();

        try
        {
            object o = b.Deserialize(s);
            c = (DocumentClass)o;
            c.CreatedFromReadFile = true;

           string objOriginal = o.GetType().AssemblyQualifiedName + "_" + o.GetType().FullName;
            string objTarget = c.GetType().AssemblyQualifiedName + "_" + c.GetType().FullName;
            bool same = (o.GetType() == c.GetType());

            if (c.DocumentTypeID <= 0)
            {
                throw new Exception("Invalid file format");
            }
        }
        catch( Exception exc )
        {
            s.Close();
            if (!useSimpleFormat)
            {
                return Read(fullFilePath, true);
            }
            throw exc;

        }
        finally
        {
            s.Close();
        }
        return c;
    }

【问题讨论】:

    标签: c# serialization debugging mode


    【解决方案1】:

    哦不...我是个白痴...

    类的一些字段在运行时模式下被混淆了......

    • 一头扎进桌子*

    对不起各位...谢谢大家的帮助...

    【讨论】:

      【解决方案2】:

      听起来你在使用条件编译,例如:

      class Foo {
      #if DEBUG
        int Bar;
      #endif
      }
      

      如果是这样,您将无法自动反序列化它。

      那么你有两个选择。

      1. 不要对序列化类型使用条件编译 - 或 -
      2. 通过添加可序列化构造函数来提供自定义序列化程序。

      【讨论】:

      • 我们没有使用条件编译...我刚刚搜索了整个 VS 项目,但没有“#if DEBUG”行...感谢您提供可能的解决方案...
      【解决方案3】:

      首先是简单的问题 - 您是否在运行时和调试模式下使用相同的凭据执行?

      【讨论】:

      • 我不知道我是否完全理解你的意思,但我们正在以同一个 Windows 用户身份运行和调试...
      猜你喜欢
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2013-12-04
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多