zoukankan      html  css  js  c++  java
  • C#实现序列化和反序列化


           从我们面试准备上面,我知道了一个知识点,就是我们vs提供的序列化方法有两个,一个叫二进制序列化,一个叫做xml序列化,下面我们说一下二进制序列化的C#实现:


    反序列化:        

    public static T Desrialize<T>(T obj, string str)
            {
                try
                {
                    obj = default(T);
                    IFormatter formatter = new BinaryFormatter();
                    byte[] buffer = Convert.FromBase64String(str);
                    MemoryStream stream = new MemoryStream(buffer);
                    obj = (T)formatter.Deserialize(stream);
                    stream.Flush();
                    stream.Close();
                }
                catch (Exception ex)
                {
                    //throw new Exception("反序列化失败,原因:" + ex.Message);
                    return obj;
                }
                return obj;
            }
    



    序列化:       
     public static string Serialize<T>(T obj)
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    MemoryStream stream = new MemoryStream();
                    formatter.Serialize(stream, obj);
                    stream.Position = 0;
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    stream.Flush();
                    stream.Close();
                    return Convert.ToBase64String(buffer);
                }
                catch (Exception ex)
                {
                    throw new Exception("序列化失败,原因:" + ex.Message);
                }
            }

    反序列化:
            public static T Desrialize<T>(T obj, string str)
            {
                try
                {
                    obj = default(T);
                    IFormatter formatter = new BinaryFormatter();
                    byte[] buffer = Convert.FromBase64String(str);
                    MemoryStream stream = new MemoryStream(buffer);
                    obj = (T)formatter.Deserialize(stream);
                    stream.Flush();
                    stream.Close();
                }
                catch (Exception ex)
                {
                    //throw new Exception("反序列化失败,原因:" + ex.Message);
                    return obj;
                }
                return obj;
            }



    序列化:
            public static string Serialize<T>(T obj)
            {
                try
                {
                    IFormatter formatter = new BinaryFormatter();
                    MemoryStream stream = new MemoryStream();
                    formatter.Serialize(stream, obj);
                    stream.Position = 0;
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    stream.Flush();
                    stream.Close();
                    return Convert.ToBase64String(buffer);
                }
                catch (Exception ex)
                {
                    throw new Exception("序列化失败,原因:" + ex.Message);
                }
            }
  • 相关阅读:
    【转】SpringBoot使用Nacos配置中心
    隐藏或修改nginx返回的Server信息(以及隐藏版本号)
    SQLite初识
    手把手教你用WinForm制作地图编辑工具(二)
    手把手教你用WinForm制作地图编辑工具(一)
    VS2019离线安装包制作
    Python zip函数
    Python isinstance函数
    Python type 函数- Python零基础入门教程
    Python format 函数- Python零基础入门教程
  • 原文地址:https://www.cnblogs.com/DoubleEggs/p/5747151.html
Copyright © 2011-2022 走看看