• 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


    }

    }

  • 相关阅读:
    8.24Java入门--->第二十六节
    IDEA-->右键没有创建包选项--->新建包不能自动扩展
    8.21Java入门--->第二十五节(多线程)
    个人冲刺(一)——体温上报app(二阶段)
    个人作业——家庭记账本
    个人冲刺(五)——家庭记账本
    个人冲刺(四)——家庭记账本
    个人冲刺(三)——家庭记账本
    个人冲刺(二)——家庭记账本
    个人冲刺(一)——家庭记账本
  • 原文地址:https://www.cnblogs.com/yuesebote/p/9316207.html
Copyright © 2020-2023  润新知