Binary Deserialization with different assembly version

前端 未结 5 697
名媛妹妹
名媛妹妹 2020-12-14 07:07

I have a project which uses BinaryFormatter to serialize a collection of structs with string and bool? datatypes.

The serialization/deserialization works fine, howev

相关标签:
5条回答
  • 2020-12-14 07:33

    You can change your BinaryFormatter property AssemblyFormat to make serialization independent of assembly version.

    // Example
    var binFormat = new BinaryFormatter();
    binFormat.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
    
    0 讨论(0)
  • 2020-12-14 07:43

    There are altenative (binary) serialization engines (like this) that aren't assembly dependent.

    0 讨论(0)
  • 2020-12-14 07:47

    Hook into the AppDomain.OnAssemblyResolve event and fix-up the assembly names

    private System.Reflection.Assembly OnAssemblyResolve( System.Object sender, System.ResolveEventArgs reArgs )
    {
         foreach( System.Reflection.Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies() ) 
         {
             System.Reflection.AssemblyName assemblyName = assembly.GetName();
             if( assemblyName.FullName == reArgs.Name ) 
             {
                  return( assembly );
             }
         }
    }
    

    source: http://osdir.com/ml/windows.devel.dotnet.clr/2003-12/msg00441.html

    0 讨论(0)
  • 2020-12-14 07:53

    You can control how the binary formatter resolves its types by assigning a custom SerializationBinder to the formatter. In this way, you won't need to mess with the AppDomain's resolve events and you eliminate the risk of unexpected side effects from that.

    There is a detailed example at MSDN.

    0 讨论(0)
  • 2020-12-14 07:53

    The GAC is your first resource, allowing different versions of the assembly to co-exist side-by-side. But that doesn't really solve anything unless your app is version tolerant too. Binary serialization has several features to handle version tolerant serialization. Read about it in this MSDN library article.

    0 讨论(0)
提交回复
热议问题