在调试模式下反序列化?

Run*_*CMD 0 c# debugging serialization mode

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

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

现在我知道你会说:那是因为他们有不同的程序集。但是我们使用自定义 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;
    }
Run Code Online (Sandbox Code Playgroud)

Run*_*CMD 5

哦不……我是个白痴……

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

  • 头撞桌子*

对不起人们...感谢所有的帮助...