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).
AppDomain.TypeResolve.
AppDomain.TypeResolve
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); }
BinaryFormatter , AssemblyFormat FormatterAssemblyStyle.Simple. , .
AssemblyFormat
FormatterAssemblyStyle
SerializationBinder BinaryFormatter. .NET 4.0 BindToName SerializationBinder, .
SerializationBinder
BindToName
Source: https://habr.com/ru/post/1795441/More articles:Extension Methods - c #Why does Nhibernate request the entire collection to remove a child? - nhibernateChoose a method sort order in Eclipse autocomplete clauses? - javaWhat happens if you declare a variable inside a macro? - c ++Is there a way to make a web request in C # that does not throw an exception in the 4xx and 5xx status codes? - c #Atomic action - mutex - c ++Delphi 6 does not cause breakpoints - delphiMaintain data compatibility with .NET Serialized when moving classes - soapHow can I use dynamic Lambda in dynamic LINQ - lambdaTSQL - connection using full-text containers - sql-serverAll Articles