The task is to write a class with methods that will serialize, deserialize any object into an array of bytes and vice versa. I wrote a serialization (not sure if this is correct)
static byte[] Serialize<T>(T obj) where T : class { if (obj == null) { return null; } using (var stream = new MemoryStream()) { DataContractSerializer ser = new DataContractSerializer(typeof(T)); ser.WriteObject(stream, obj); return stream.ToArray(); } } An example is, for example, here , but they use an xml file, and I need it without it. How to write serialization / deserialization without a file? Will I get object bytes from a stream?
DataContractSerializerserializes to xml, which is then written to the stream. If you need an array of bytes, it is probably best to use binary serialization. For example,BinaryFormatter. - Alexander Petrov