zoukankan      html  css  js  c++  java
  • model拷贝

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Xml;
    using System.Xml.Serialization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using System.Collections.ObjectModel;
    namespace Utility
    {
    public class SerializeHelper
    {
    #region Object C Binary
    public static String SerializeByte(Object obj)
    {
    if (obj == null) return null;
    IFormatter formatter = new BinaryFormatter();
    Byte[] b;
    using (MemoryStream ms = new MemoryStream())
    {
    formatter.Serialize(ms, obj);
    b = ms.ToArray();
    ms.Close();
    ms.Dispose();
    }
    return Convert.ToBase64String(b);
    }

    public static String SerializeByte<T>(Object obj)
    {
    if (obj == null) return null;
    IFormatter formatter = new BinaryFormatter();
    Byte[] b;
    using (MemoryStream ms = new MemoryStream())
    {
    formatter.Serialize(ms, obj);
    b = ms.ToArray();
    ms.Close();
    ms.Dispose();
    }
    return Convert.ToBase64String(b);
    }

    public static Object DeserializeByte(String data)
    {
    if (string.IsNullOrEmpty(data) ) return null;
    byte[] BytArray = Convert.FromBase64String(data);
    Object obj;
    IFormatter formatter = new BinaryFormatter();
    using (MemoryStream ms = new MemoryStream())
    {
    ms.Write(BytArray, 0, BytArray.Length);
    ms.Position = 0;
    obj = (Object)formatter.Deserialize(ms);
    ms.Close();
    ms.Dispose();
    }
    return obj;
    }

    public static T DeserializeByteToInfo<T>(String data)
    {
    if (string.IsNullOrEmpty(data)) return default(T);
    byte[] BytArray = Convert.FromBase64String(data);
    T obj;
    IFormatter formatter = new BinaryFormatter();
    try
    {
    using (MemoryStream ms = new MemoryStream())
    {
    ms.Write(BytArray, 0, BytArray.Length);
    ms.Position = 0;
    obj = (T)formatter.Deserialize(ms);
    ms.Close();
    ms.Dispose();
    }
    }
    catch { return default(T); }
    return obj;
    }

    #endregion


    ///对象副本拷贝
    /// <summary>
    /// /对象副本拷贝
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="OldObjectList">原数据</param>
    /// <param name="NewObjectList">新数据</param>
    public static void CopyObjectList<T>(T OldObjectList,ref T NewObjectList)
    {
    using (var stream = new System.IO.MemoryStream())
    {
    var SerializationFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    if (OldObjectList != null)
    {
    SerializationFormat.Serialize(stream, OldObjectList);

    stream.Position = 0;

    NewObjectList = (T)SerializationFormat.Deserialize(stream);
    }
    else
    {
    NewObjectList = default(T);
    }
    }
    }

    #region BinaryFormatter序列化
    /// <summary>
    /// BinaryFormatter序列化
    /// </summary>
    /// <param name="item">对象</param>
    public static string ToBinary<T>(T item)
    {
    BinaryFormatter formatter = new BinaryFormatter();
    using (MemoryStream ms = new MemoryStream())
    {
    formatter.Serialize(ms, item);
    ms.Position = 0;
    byte[] bytes = ms.ToArray();
    StringBuilder sb = new StringBuilder();
    foreach (byte bt in bytes)
    {
    sb.Append(string.Format("{0:X2}", bt));
    }
    return sb.ToString();
    }
    }

    /// <summary>
    /// BinaryFormatter反序列化
    /// </summary>
    /// <param name="str">字符串序列</param>
    public static T FromBinary<T>(string str)
    {


    int intLen = str.Length / 2;
    byte[] bytes = new byte[intLen];
    for (int i = 0; i < intLen; i++)
    {
    int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
    bytes[i] = (byte)ibyte;
    }
    BinaryFormatter formatter = new BinaryFormatter();
    using (MemoryStream ms = new MemoryStream(bytes))
    {
    return (T)formatter.Deserialize(ms);
    }
    }
    #endregion


    }

    }

  • 相关阅读:
    搭建Linux Kettle服务端重装系统软件或帮助
    JFrame加载Browser,Jxbrowser导入
    Bonita了解
    基于用户控件固定菜单MenuStrip/ToolStripMenuItem列表及动态菜单列表,是上一篇文章的综合和升级
    winfrom打开本地默认浏览器或打开IE浏览器
    Winfrom动态添加MenuStrip菜单,同时绑定反射事件,可扩展后期动态配置
    给GroupBox动态添加一个按钮,实现展开和折叠功能
    Rest导出Excel文件流
    List<HashMap>排序,List内存分页
    解决idea项目部署到Tomcat时Artiface没有文件
  • 原文地址:https://www.cnblogs.com/yuesebote/p/9316207.html
Copyright © 2011-2022 走看看