Tolerant Serialization Version - How to Find the AssemblyName of the Original

In .NET 2.0 (and, I believe, at the top), the Tolerant Serialization version will successfully deserialize the serialized object from the older version of the assembly that contains this object.

When I open such a binary formatted serialized stream using a hexadecimal viewer (a simple drag'ndrop in VS will be), I can see the assembly information contained in this stream there.

Is there a way to get this information during deserialization? This can be used, for example, to apply corrections to known issues when reading in older content.

UPDATE: It seems that this cannot be done (except for changing the class itself, as in Paul Bets’s answer, it also didn’t check), is there another way to read this value? Is binary format displayed?

+3
source share
3 answers

I discovered these serialization problems first hand by writing this CodeProject article (go to the section "Loading a directory from disk" about halfway down).

Basically I am serializing something with an ASP.NET application - and the serialized data could not be read after the IIS application restarted (due to all the dynamic compilation / temporary assembly cache / etc, which ASP.NET does)! Oh!

, - , ,

h4octhiw, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null

, , , , "-". ( , ) (, "" )... ..

, ( ). System.Runtime.Serialization.SerializationBinder: .

public class CatalogBinder: System.Runtime.Serialization.SerializationBinder
{
    public override Type BindToType (string assemblyName, string typeName) 
    { 
        // get the 'fully qualified (ie inc namespace) type name' into an array
        string[] typeInfo = typeName.Split('.');
        // because the last item is the class name, which we're going to 
        // 'look for' in *this* namespace/assembly
        string className=typeInfo[typeInfo.Length -1];
        if (className.Equals("Catalog"))
        {
            return typeof (Catalog);
        }
        else if (className.Equals("Word"))
        {
            return typeof (Word);
        }
        if (className.Equals("File"))
        {
            return typeof (File);
        }
        else
        {    // pass back exactly what was passed in!
            return Type.GetType(string.Format( "{0}, {1}", typeName, 
                                assemblyName));
        }
    } 
}

BindToType "" , . typeName, assemblyName, , , , SerializationBinder, , , , "" .

FYI, "" :

System.Runtime.Serialization.IFormatter formatter = 
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Binder = new CatalogBinder(); // THIS IS THE IMPORTANT BIT
object deserializedObject = formatter.Deserialize(stream); 
+5

, AssemblyInfo, Assembly.GetExecutingAssembly(). FullName

+1

Lutz Roeders ( Red Gate's) Reflector.

System.Runtime.Serialization.Formatters.Binary.__BinaryParser BinaryFormatter Deserialize .

Docking in the reflector can give you an idea of ​​how to pre-read the binary header and determine version information.

0
source

Source: https://habr.com/ru/post/1709777/


All Articles