title: Is there a high performance way to replace the BinaryFormatter in .NET5?
body: <p>Before .NET5 we serialize/deserialize the Bytes/Object by these code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    private static byte[] StructToBytes&lt;T&gt;(T t)
      {
          using (var ms = new MemoryStream())
          {
              var bf = new BinaryFormatter();
              bf.Serialize(ms, t);
              return ms.ToArray();
          }
      }
  
      private static T BytesToStruct&lt;T&gt;(byte[] bytes)
      {
          using (var memStream = new MemoryStream())
          {
              var binForm = new BinaryFormatter();
              memStream.Write(bytes, 0, bytes.Length);
              memStream.Seek(0, SeekOrigin.Begin);
              var obj = binForm.Deserialize(memStream);
              return (T)obj;
          }
      }
  </code></pre>
  <p>But the BinaryFormatter will be removed for the security reason:</p>
  <p><a href="https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/binaryformatter-security-guide" rel="nofollow noreferrer">https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/binaryformatter-security-guide</a></p>
  <p>So is there some simple but high performance method to replace BinaryFormatter?</p>