C# Object Binary Serialization

前端 未结 6 1710
-上瘾入骨i
-上瘾入骨i 2020-12-05 02:17

I want to make a binary serialize of an object and the result to save it in a database.

Person person = new Person();
person.Name = \"something\";

MemoryStr         


        
相关标签:
6条回答
  • 2020-12-05 02:58

    What you're really asking for is a safe way of representing arbitrary binary data as text and then converting it back again. The fact that it stores a serialized object is irrelevant.

    The answer is almost to use Base 64 (e.g. Convert.ToBase64String and Convert.FromBase64String). Do not use Encoding.UTF8.GetString or anything similar - your binary data is not encoded text data, and shouldn't be treated as such.

    However, does your database not have a data type for binary data? Check for BLOB, IMAGE and BINARY types...

    0 讨论(0)
  • 2020-12-05 02:59

    Have you not looked into converting the memorystream into a base64hex string to be put into the database?

     byte[] mStream = memorystream.ToArray();
     string sConvertdHex = System.Convert.ToBase64String(mStream)
    

    Then you can dump the contents sConvertdHex to the database. To deserialize it you need to do the reverse

     byte[] mData = System.Convert.FromBase64String(...)
    

    then deserialize mData back to your object.

    0 讨论(0)
  • 2020-12-05 03:00
    //-------write to database-------------------------
    Person person = new Person();
    person.name = "Firstnm  Lastnm";
    MemoryStream memorystream = new MemoryStream(); 
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(memorystream, person);
    byte[] yourBytesToDb = memorystream.ToArray();
    //here you write yourBytesToDb to database
    
    
    //----------read from database---------------------
    //here you read from database binary data into yourBytesFromDb
    MemoryStream memorystreamd = new MemoryStream(yourBytesFromDb);
    BinaryFormatter bfd = new BinaryFormatter();
    Person deserializedperson = bfd.Deserialize(memorystreamd) as Person;
    
    0 讨论(0)
  • 2020-12-05 03:01

    Basically, don't save the data as string to the database, there are blob fields available to store binary data.

    If you really need to have the data as string, you'll need to convert your byte[] to a string using base64 encoding, and to grab the byte[] from a string use decoding.

    0 讨论(0)
  • 2020-12-05 03:03

    I used something like this

    MemoryStream memoryStream = new MemoryStream();
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(memoryStream, Person);
    memoryStream.Flush();
    memoryStream.Position = 0;
    string value = Convert.ToBase64String(memoryStream.ToArray());
    
    0 讨论(0)
  • 2020-12-05 03:12

    Here's the sample. TData must be marked [Serializable] and all fields type also.

        private static TData DeserializeFromString<TData>(string settings)
        {
            byte[] b = Convert.FromBase64String(settings);
            using (var stream = new MemoryStream(b))
            {
                var formatter = new BinaryFormatter();
                stream.Seek(0, SeekOrigin.Begin);
                return (TData)formatter.Deserialize(stream);
            }
        }
    
        private static string SerializeToString<TData>(TData settings)
        {
            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, settings);
                stream.Flush();
                stream.Position = 0;
                return Convert.ToBase64String(stream.ToArray());
            }
        }
    
    0 讨论(0)
提交回复
热议问题