Changing the namespace of binary serializations

I serialized the class that was used in the Temp namespace, but now I am deserializing inside another namespace (I mean that the class that I use to retrieve objects is currently in a different namespace). I encountered an error that the Temp namespace was not found. I found this mapping useful: Maintain .NET Serialized data compatibility when moving classes .

Is there a way to simply serialize a class object, not an assembly or namespace information? (I think about future changes and get rid of this comparison).

+3
source share
3 answers

AppDomain.TypeResolve.

+2

Binder. (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.serializationbinder.aspx)

, :

sealed class MyBinder : SerializationBinder
{
    private readonly Type _type;

    public MyBinder(Type type)
    {
        _type = type;
    }

    public override Type BindToType(string assemblyName, string typeName)
    {
        return _type;
    }
}

BinaryFormatter

var formatter = new BinaryFormatter();

formatter.Binder = new MyBinder(typeof(YourClass));

using (var stream = new MemoryStream(bytes))
{
    YourClass yourobject = formatter.Deserialize(stream);
}
+4

BinaryFormatter , AssemblyFormat FormatterAssemblyStyle.Simple. , .

SerializationBinder BinaryFormatter. .NET 4.0 BindToName SerializationBinder, .

+2

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


All Articles