8

I have a bit of C# code which I use for performing a deep copy of an object:

    public static T Copy<T>(T objectToCopy)
    {
        T result = default(T);

        using (var memoryStream = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(memoryStream, objectToCopy);
            memoryStream.Seek(0, SeekOrigin.Begin);
            result = (T)formatter.Deserialize(memoryStream);
            memoryStream.Close();
        }

        return result;
    }

I get this warning from Visual Studio:

Warning SYSLIB0011
'BinaryFormatter.Serialize(Stream)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.'

I get the same warning about BinaryFormatter.Deserialize(Stream).

I've looked at the suggested link, and they list some preferred alternatives:

  • XmlSerializer and DataContractSerializer to serialize object graphs into and from XML. Do not confuse DataContractSerializer with NetDataContractSerializer.
  • BinaryReader and BinaryWriter for XML and JSON.
  • The System.Text.Json APIs to serialize object graphs into JSON.

I'm just struggling to figure out in my specific case how I would implement one of those alternatives.

If anyone could assist me in this I would greatly appreciate it.

Thank you.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • you can use `System.Text.Json` to serialize/deserialize stream looks here : https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0, especially `SerializeAsyn/DeserializeAsync` – Mohammed Sajid Jan 14 '21 at 08:56
  • 2
    JSON is text, so it's not a direct alternative. A better option is the Protocol Buffers format used by gRPC, supported by .NET's gRPC tooling or StackExchange's protobuf-net library – Panagiotis Kanavos Jan 14 '21 at 09:36
  • 3
    As for your case - you aren't serializing anything. You're using BinaryFormatter as a hack to create a deep clone for an object. That's still a hack and can easily end up producing bad copies - did you really need to *clone* that Singleton instance that was stored in a private field? Because that's what your current method does – Panagiotis Kanavos Jan 14 '21 at 09:39
  • @PanagiotisKanavos pedantic point, perhaps; but protobuf-net is not owned/affiliated/etc with Stack Exchange in any way and never has been (it predates them) – Marc Gravell Jan 14 '21 at 09:39
  • 1
    In the general case, the **only** correct way to do a deep clone of general objects, is to involve the objects or in some other way create per-type specific deep cloning implementations. I say this because **in the general case**, there can be plenty of stuff found inside objects that should not be cloned. If none of this applies, use one of the libraries you've mentioned, json or protobuf or whatnot. – Lasse V. Karlsen Jan 14 '21 at 09:44
  • @MarcGravell interrupting someone's sales pitch is bad form. Prepare for Top Gear jokes – Panagiotis Kanavos Jan 14 '21 at 09:51

0 Answers0