convert dictionary or list to byte[]

后端 未结 2 1056
慢半拍i
慢半拍i 2020-12-13 21:39

Ok, i\'ve seen many similar questions both on here and unity forums asking about converting from one format to another. I\'ve got a (hopefully) simple question that i just c

2条回答
  •  既然无缘
    2020-12-13 22:31

    You may want to try serialization.

    var binFormatter = new BinaryFormatter();
    var mStream = new MemoryStream();
    binFormatter.Serialize(mStream, myObjToSerialize);
    
    //This gives you the byte array.
    mStream.ToArray();
    

    And then if you want to turn the byte array back into an object:

    var mStream = new MemoryStream();
    var binFormatter = new BinaryFormatter();
    
    // Where 'objectBytes' is your byte array.
    mStream.Write (objectBytes, 0, objectBytes.Length);
    mStream.Position = 0;
    
    var myObject = binFormatter.Deserialize(mStream) as YourObjectType;
    

提交回复
热议问题