序列化

  • 1358
  • 0

摘要:序列化

引用


using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

反序列化


public static object Deserialize(byte[] data)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream(data, 0, data.Length);
                object tmp = formatter.Deserialize(stream);
                 return tmp;
            }
            catch (Exception ex) { ConsoleSay(_className, "Deserialize", ex); }
            return null ;
        }

序列化


public static byte[] Serialize(object map)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, map);
            byte[] bBuffer = ms.GetBuffer();
            ms.Close();
            return bBuffer;
        }

強轉


list<int> lst = (list<int>) object;

object copy


public static List  NewCopy(List  plst)
        {
            try
            {
                byte[] buff = new byte[1024000];
                MemoryStream ms = new MemoryStream(buff);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, plst);
                ms.Seek(0, SeekOrigin.Begin);
                plst = (List)formatter.Deserialize(ms);
                return plst;
            }
            catch
            {
            }
            return new List();
        }

===================

搞了C#

現在來學學維修筆電