Convert an array of different value types to a byte array

后端 未结 3 738
花落未央
花落未央 2020-12-21 22:41

This is what I\'ve come up with so far, but it doesn\'t seem very optimal, any ideas on better approaches?

public void ToBytes(object[] data, byte[] buffer)
         


        
3条回答
  •  生来不讨喜
    2020-12-21 23:22

    Probably, you should consider using BinaryFormatter instead:

    var formatter = new BinaryFormatter();
    var stream = new MemoryStream();
    formatter.Serialize(stream, obj);
    byte[] result = stream.ToArray();
    

    Beside that, there are some pretty good serialization frameworks like Google Protocol Buffers if you want to avoid reinventing the wheel.

提交回复
热议问题