Binary Formatter serialize-deserialize

101 Views Asked by At

I have 2 voids to serialize/Deserialize my class data (what is serializable)

But sometimes i get error: TableDataObject translater: The input stream is not a valid binary format. The starting contents (in bytes) are: 21-31-83-C6-7A-60-03-F8-52-B7-87-2C-5A-DE-38-A5-8F ...

2 voids:

public static byte[] GetBytes(TableData obj)
    {
        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, obj);
                ms.Close();
                return ms.ToArray();
            }
        }
        catch (Exception ex)
        {
            Debug.Log("Byte maker: "+ ex.Message);
            return null;
        }
        
    }
    public static TableData GetObject(byte[] arrBytes)
    {
        try
        {
            using (var memStream = new MemoryStream())
            {
                var binForm = new BinaryFormatter();
                memStream.Write(arrBytes, 0, arrBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                TableData obj = (TableData)binForm.Deserialize(memStream);
                return obj;
            }
        }
        catch (Exception ex)
        {
            Debug.Log("TableDataObject translater: "+ ex.Message);
            return null;
        }
        
    }

I m expecting 0 error because it s an easy task

0

There are 0 best solutions below