序列化对象与反序列化对象

序列化对象

public string SerializeObject(object obj)
     {
         IFormatter formatter = new BinaryFormatter();
         string result = string.Empty;
         using (MemoryStream stream = new MemoryStream())
         {
             formatter.Serialize(stream, obj);
             byte[] byt = new byte[stream.Length];
             byt = stream.ToArray();
             result = Convert.ToBase64String(byt);
             stream.Flush();
         }
         return result;
     }

反序列化对象

public object DeserializeObject(string str)
      {
          IFormatter formatter = new BinaryFormatter();
          byte[] byt = Convert.FromBase64String(str);
          object obj = null;
          using (Stream stream = new MemoryStream(byt, 0, byt.Length))
          {
              obj = formatter.Deserialize(stream);
          }
          return obj;
      }

猜你喜欢

转载自www.cnblogs.com/HarryChis/p/10391008.html