C# 调试模式下的反序列化?

C# 调试模式下的反序列化?,c#,serialization,debugging,mode,C#,Serialization,Debugging,Mode,有谁能解释为什么会发生以下情况: 在调试模式下序列化文件时,可以在调试模式下再次打开它,但不能在运行时打开。 在运行时模式下序列化文件时,可以在运行时模式下再次打开它,但不能在调试模式下打开 现在我知道你会说:那是因为他们有不同的组件。 但我们使用定制活页夹,如下所述。。。 此外,如果我们比较这两种类型,“bool same=(o.GetType()==c.GetType())”,结果总是“true” 那为什么我们不能打开文件 public class Binder : Serializatio

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

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

现在我知道你会说:那是因为他们有不同的组件。 但我们使用定制活页夹,如下所述。。。 此外,如果我们比较这两种类型,“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;
    }
公共类绑定器:SerializationBinder{
公共重写类型BindToType(字符串assemblyName、字符串typeName){
类型tyType=null;
字符串sShortAssemblyName=assemblyName.Split(',')[0];
程序集[]ayAssemblies=AppDomain.CurrentDomain.GetAssemblies();
if(sshortasemblyname.ToLower()=“debugName”)
{
sshortasemblyname=“runtimeName”;
}
foreach(装配组件中的装配组件){
if(sShortAssemblyName==ayAssembly.FullName.Split(',')[0]){
tyType=ayAssembly.GetType(typeName);
打破
}
}
返回类型;
}
}
公共静态文档类读取(字符串fullFilePath,bool-useSimpleFormat)
{
DocumentClass c=新的DocumentClass();
c、 CreatedFromReadFile=true;
streams=File.OpenRead(fullFilePath);//f.Open(FileMode.Open);
BinaryFormatter b=新的BinaryFormatter();
如果(使用SimpleFormat)
{
b、 AssemblyFormat=System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
}
b、 活页夹=新活页夹();
尝试
{
对象o=b。反序列化;
c=(文档类)o;
c、 CreatedFromReadFile=true;
字符串objOriginal=o.GetType().AssemblyQualifiedName+“933;”+o.GetType().FullName;
字符串objTarget=c.GetType().AssemblyQualifiedName+“933;”+c.GetType().FullName;
bool same=(o.GetType()==c.GetType());

如果(c.DocumentTypeID听起来像是在使用条件编译,例如:

class Foo {
#if DEBUG
  int Bar;
#endif
}
如果是这样,您将无法自动反序列化它

那么你有两个选择

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

  • 听起来您正在使用条件编译,例如:

    class Foo {
    #if DEBUG
      int Bar;
    #endif
    }
    
    如果是这样,您将无法自动反序列化它

    那么你有两个选择

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

  • 简单问题优先-您在运行时和调试模式下都使用相同的凭据执行吗?

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

    哦,不……我是个白痴

    类的某些字段在运行时模式下被混淆

    • 直击桌子*

    对不起,大家……谢谢你们的帮助……

    哦,不……我是个白痴

    类的某些字段在运行时模式下被混淆

    • 直击桌子*

    抱歉,各位…感谢所有的帮助…

    我们没有使用条件编译…我刚刚搜索了整个VS项目,没有出现“#if DEBUG”行…不过感谢您提供了一个可能的解决方案…我们没有使用条件编译…我刚刚搜索了整个VS项目,没有出现“#if DEBUG”行…感谢您提供可能的解决方案..我不知道我是否完全理解您的意思,但我们正在以同一个Windows用户的身份运行和调试…我不知道我是否完全理解您的意思,但我们正在以同一个Windows用户的身份运行和调试。。。