2013-02-16 106 views
4

我想序列化我的对象图形为一个字符串,然后从字符串反序列化它。如果我这样做使用BinaryFormatter序列化和反序列化对象图

using (var memStream = new System.IO.MemoryStream()) 
{ 
    mf.Serialize(memStream, this); 
    memStream.Seek(0, 0); 

    Search s; 
    using (var memStrClone = new System.IO.MemoryStream()) 
    { 
      memStream.CopyTo(memStrClone); 
      memStrClone.Seek(0, 0); 
      s = mf.Deserialize(memStrClone) as Search; 
    } 
} 

上面的代码工作,但序列化到一个字符串,并尝试反序列化这样

Search s; 
string xml = ToString<Search>(this); 
s = FromString<Search>(xml); 

public static TType FromString<TType>(string input) 
{ 
    var byteArray = Encoding.ASCII.GetBytes(input); 
    using (var stream = new MemoryStream(byteArray)) 
    { 
      var bf = new BinaryFormatter(); 
      return (TType)bf.Deserialize(stream); 
    } 
} 

public static string ToString<TType>(TType data) 
{ 
    using (var ms = new MemoryStream()) 
    { 
      var bf = new BinaryFormatter(); 
      bf.Serialize(ms, data); 
      return Encoding.ASCII.GetString(ms.GetBuffer()); 
    } 
} 

相同的字符串抛出一个异常对象序列化就好

对象类型'1936026741 Core.Sebring.BusinessObjects.Search.Search'没有程序集标识。

任何帮助,非常感谢。谢谢。

回答

8

下面是一些代码,它将做你想做的事(我认为) - 但我必须问 - 为什么你想序列化成这样的字符串?

如果该类足够简单,可以串行化为一个字符串,则使用一个XML序列化程序更容易处理;如果你想将它序列化到磁盘上,二进制将它写出到一个文件中,如果它很复杂并且你正在序列化它来传输 - 可以考虑使用类似protobuf-net的东西。

我认为你问题的症结在于你正在尝试使用ASCII编码 - 我使用的是Base64编码。

反正 - 这里去(我刚刚在你的搜索类的猜测!)

class Program 
{ 
    [Serializable] 
    public class Search 
    { 
     public Guid ID { get; private set; } 

     public Search() { } 

     public Search(Guid id) 
     { 
      ID = id; 
     } 

     public override string ToString() 
     { 
      return ID.ToString(); 
     } 
    } 

    static void Main(string[] args) 
    { 
     Search search = new Search(Guid.NewGuid()); 
     Console.WriteLine(search); 
     string serialized = SerializeTest.SerializeToString(search); 
     Search rehydrated = SerializeTest.DeSerializeFromString<Search>(serialized); 
     Console.WriteLine(rehydrated); 

     Console.ReadLine(); 
    } 
} 

public class SerializeTest 
{ 
    public static Encoding _Encoding = Encoding.Unicode; 

    public static string SerializeToString(object obj) 
    { 
     byte[] byteArray = BinarySerializeObject(obj); 
     return Convert.ToBase64String(byteArray); 
    } 

    public static T DeSerializeFromString<T>(string input) 
    { 
     byte[] byteArray = Convert.FromBase64String(input); 
     return BinaryDeserializeObject<T>(byteArray); 
    } 

    /// <summary> 
    /// Takes a byte array and deserializes it back to its type of <see cref="T"/> 
    /// </summary> 
    /// <typeparam name="T">The Type to deserialize to</typeparam> 
    /// <param name="serializedType">The object as a byte array</param> 
    /// <returns>The deserialized type</returns> 
    public static T BinaryDeserializeObject<T>(byte[] serializedType) 
    { 
     if (serializedType == null) 
      throw new ArgumentNullException("serializedType"); 

     if (serializedType.Length.Equals(0)) 
      throw new ArgumentException("serializedType"); 

     T deserializedObject; 

     using (MemoryStream memoryStream = new MemoryStream(serializedType)) 
     { 
      BinaryFormatter deserializer = new BinaryFormatter(); 
      deserializedObject = (T)deserializer.Deserialize(memoryStream); 
     } 

     return deserializedObject; 
    } 

    /// <summary> 
    /// Takes an object and serializes it into a byte array 
    /// </summary> 
    /// <param name="objectToSerialize">The object to serialize</param> 
    /// <returns>The object as a <see cref="byte"/> array</returns> 
    public static byte[] BinarySerializeObject(object objectToSerialize) 
    { 
     if (objectToSerialize == null) 
      throw new ArgumentNullException("objectToSerialize"); 

     byte[] serializedObject; 

     using (MemoryStream stream = new MemoryStream()) 
     { 
      BinaryFormatter formatter = new BinaryFormatter(); 
      formatter.Serialize(stream, objectToSerialize); 
      serializedObject = stream.ToArray(); 
     } 

     return serializedObject; 
    } 

} 

HTH

+1

+1为Base64编码(用于在ASCII字符串格式的二进制数据)。 – 2013-02-16 22:04:00

+0

我序列化为字符串,以便我可以将其存储在数据库中。如果有更好的方法可以提出建议。 – andrewramka 2013-02-16 23:57:48

+0

您是否可以更新原始问题以包含搜索课程?当我不知道你想要序列化的时候,很难建议。 – Jay 2013-02-17 09:36:39