zoukankan      html  css  js  c++  java
  • 序列化、反序列化对象的用法

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
     
    namespace MyProj.Base.Srv
    {
        /// <summary>
        /// 处理数据 服务类
        /// </summary>
        public class BigDataBaseSrv
        {
            /// <summary>
            /// 读二进制数据,转换成相应的类型列表
            /// </summary>
            public List<T> ReadBytes<T>(byte[] argBytes) where T : class
            {
                List<T> list = null; //new List<T>();
                try
                {
                    BinaryFormatter ser = new BinaryFormatter();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(argBytes, 0, argBytes.Length);
                    ms.Position = 0;
                    object obj = ser.Deserialize(ms);
                    ms.Close();
                    list = (List<T>)obj;
                }
                catch (Exception)
                {
                }
                return list;
            }
            /// <summary>
            /// 读二进制数据,转换成 字符串
            /// </summary>
            public string ReadBytes(byte[] argBytes)
            {
                string res = "";
                try
                {
                    BinaryFormatter ser = new BinaryFormatter();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(argBytes, 0, argBytes.Length);
                    ms.Position = 0;
                    object obj = ser.Deserialize(ms);
                    ms.Close();
                    res = (string)obj;
                }
                catch (Exception)
                {
                }
                return res;
            }
            public byte[] SerializeList<T>(List<T> argList) where T : class
            {
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                ser.Serialize(ms, argList);
                byte[] buffer = ms.ToArray();
                ms.Close();
                return buffer;
            }
     
            public byte[] SerializeString(string argStr)
            {
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                ser.Serialize(ms, argStr);
                byte[] buffer = ms.ToArray();
                ms.Close();
                return buffer;
            }
        }
    }
  • 相关阅读:
    白书上的BellmanFord模板
    c#中的分部类和分部方法
    c#类
    浪潮gs开发平台学习平台快速开发入门
    c#学习积累
    自定义属性编辑器
    hibernate 中的hql,nativesql ,list(),iterate() 的使用规则
    c#继承
    浪潮gs中间件服务器,客户端,数据库sqlserver安装注意事项
    c#接口
  • 原文地址:https://www.cnblogs.com/jx270/p/4583699.html
Copyright © 2011-2022 走看看