C#对象深拷贝DeepClone
#region 程序集 Kingdee.BOS, Version=7.7.2388.8, Culture=neutral, PublicKeyToken=null // D:\WorkSpace\K3Cloud\BIN\Kingdee.BOS.dll // Decompiled with ICSharpCode.Decompiler 3.1.0.3652 #endregion using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace Kingdee.BOS.Util { public static class ObjectUtils { /// <summary> /// 判断指定的对象是否是空引用,或空字符串。 /// </summary> /// <param name="value">需要测试的对象</param> /// <returns>如果 value 参数为空引用或空字符串 (""),则为 true;否则为 false。</returns> public static bool IsNullOrEmpty(this object value) { if (value != null && value != DBNull.Value && value.ToString().Length != 0) { return false; } return true; } /// <summary> /// 判断指定的对象是否是空引用,或空字符串, 或空白字符。 /// </summary> /// <param name="value">需要测试的对象</param> /// <returns>如果 value 参数为空引用或空字符串 ("")或空白字符,则为 true;否则为 false。</returns> public static bool IsNullOrEmptyOrWhiteSpace(this object value) { if (value != null && value != DBNull.Value && value.ToString().Length != 0 && !string.IsNullOrWhiteSpace(value.ToString())) { return false; } return true; } /// <summary> /// 为对象创建一个拷贝 /// </summary> /// <param name="obj">需要创建拷贝的对象</param> /// <returns>新建立的拷贝对象</returns> public static object CreateCopy(object obj) { return CreateCopy(obj, FormatterType.BinaryFormatter); } /// <summary> /// 为对象创建一个拷贝 /// </summary> /// <param name="obj">需要创建拷贝的对象</param> /// <param name="formatterType">格式类型</param> /// <returns>新建立的拷贝对象</returns> public static object CreateCopy(object obj, FormatterType formatterType) { //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Expected O, but got Unknown if (obj == null) { return null; } object obj2 = null; using (MemoryStream memoryStream = new MemoryStream()) { IFormatter formatter = null; formatter = ((formatterType != FormatterType.NetDataContract) ? new BinaryFormatter() : ((IFormatter)new NetDataContractSerializer())); formatter.Serialize(memoryStream, obj); memoryStream.Position = 0L; obj2 = formatter.Deserialize(memoryStream); memoryStream.Close(); memoryStream.Dispose(); formatter = null; return obj2; } } /// <summary> /// 将对象序列化为字节流 /// </summary> /// <param name="obj">需要序列化的对象</param> /// <returns>序列化后的字节流</returns> public static byte[] GetObjectSream(object obj) { if (obj == null) { return null; } byte[] array = null; using (MemoryStream memoryStream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(memoryStream, obj); array = memoryStream.ToArray(); memoryStream.Close(); memoryStream.Dispose(); return array; } } /// <summary> /// 将字节流反序列化为对象 /// </summary> /// <param name="bytes">需要反序列化的字节流</param> /// <returns>反序列化后的对象</returns> public static object GetObject(byte[] bytes) { if (bytes == null) { return null; } object obj = null; using (MemoryStream memoryStream = new MemoryStream(bytes)) { IFormatter formatter = new BinaryFormatter(); obj = formatter.Deserialize(memoryStream); memoryStream.Close(); memoryStream.Dispose(); return obj; } } /// <summary> /// 判断返回主键值是否是一个无效的值。 /// </summary> /// <param name="pkValue">主键值</param> /// <returns>主键值是一个无效的值,则为 true;否则为 false。</returns> public static bool IsEmptyPrimaryKey(this object pkValue) { if (pkValue == null) { return true; } string text = pkValue as string; if (text != null) { return string.IsNullOrWhiteSpace(text); } if (pkValue is int) { return (int)pkValue == 0; } if (pkValue is long) { return (long)pkValue == 0; } if (pkValue.GetType().IsValueType) { object objA = Activator.CreateInstance(pkValue.GetType()); return object.Equals(objA, pkValue); } return false; } /// <summary> /// 返回字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string Object2String(object obj) { if (obj != null) { return obj.ToString(); } return ""; } /// <summary> /// 得到对象对应的数值 /// </summary> /// <param name="obj">要转换的对象</param> /// <param name="iDefaultValue">默认值</param> /// <returns>得到的int值</returns> public static int Object2Int(object obj, int iDefaultValue = 0) { if (obj.IsNullOrEmptyOrWhiteSpace()) { return iDefaultValue; } try { return Convert.ToInt32(obj); } catch { return iDefaultValue; } } /// <summary> /// 得到对象对应的数值 /// </summary> /// <param name="obj">要转换的对象</param> /// <param name="iDefaultValue">默认值</param> /// <returns>得到的bool值</returns> public static bool Object2Bool(object obj, bool iDefaultValue = false) { if (obj.IsNullOrEmptyOrWhiteSpace()) { return iDefaultValue; } try { return Convert.ToBoolean(obj); } catch { return iDefaultValue; } } /// <summary> /// 得到对象对应的数值 /// </summary> /// <param name="obj">要转换的对象</param> /// <param name="iDefaultValue">默认值</param> /// <returns>得到的int值</returns> public static long Object2Int64(object obj, long iDefaultValue = 0L) { if (obj.IsNullOrEmptyOrWhiteSpace()) { return iDefaultValue; } try { return Convert.ToInt64(obj); } catch { return iDefaultValue; } } /// <summary> /// 得到对象对应的数值 /// </summary> /// <param name="obj">要转换的对象</param> /// <param name="iDefaultValue">默认值</param> /// <returns>得到的int值</returns> public static decimal ToDecimal(object obj, decimal iDefaultValue = default(decimal)) { if (obj.IsNullOrEmptyOrWhiteSpace()) { return iDefaultValue; } try { return Convert.ToDecimal(obj); } catch { return iDefaultValue; } } /// <summary> /// 判断FormId是否是动态构造 /// </summary> /// <param name="formId"></param> /// <returns></returns> public static bool IsDynamicFromId(string formId) { bool result = false; int num = formId.IndexOf("@"); if (!string.IsNullOrWhiteSpace(formId) && num >= 0 && num < formId.Length) { result = true; } return result; } /// <summary> /// 获取对象的字符串,如果对象为空,返回string.Empty /// </summary> /// <param name="obj">对象</param> /// <returns>对象的字符串</returns> public static string GetString(this object obj) { if (obj != null && obj != DBNull.Value) { return obj.ToString(); } return string.Empty; } public static string Serializer(Type type, object obj) { string empty = string.Empty; using (MemoryStream memoryStream = new MemoryStream()) { XmlSerializer xmlSerializer = new XmlSerializer(type); xmlSerializer.Serialize(memoryStream, obj); memoryStream.Position = 0L; StreamReader streamReader = new StreamReader(memoryStream); return streamReader.ReadToEnd(); } } public static object Deserialize(Type type, string xml) { using (StringReader textReader = new StringReader(xml)) { XmlSerializer xmlSerializer = new XmlSerializer(type); return xmlSerializer.Deserialize(textReader); } } /// <summary> /// 通过反射浅复制 /// </summary> public static object CloneByReflection(object obj) { object obj2 = obj; if (obj != null) { Type type = obj.GetType(); obj2 = type.Assembly.CreateInstance(type.FullName); PropertyInfo[] properties = type.GetProperties(); PropertyInfo[] array = properties; foreach (PropertyInfo propertyInfo in array) { if (propertyInfo.CanRead && propertyInfo.CanWrite) { object value = propertyInfo.GetValue(obj, null); propertyInfo.SetValue(obj2, value, null); } } } return obj2; } /// <summary> /// 对象深拷贝 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static T DeepClone<T>(T obj) { using (MemoryStream memoryStream = new MemoryStream()) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, obj); memoryStream.Position = 0L; return (T)binaryFormatter.Deserialize(memoryStream); } } /// <summary> /// 得到分批次数 /// </summary> /// <param name="allCount"></param> /// <param name="everyCount"></param> /// <returns></returns> public static int GetBatchTimes(int allCount, int everyCount) { return allCount / everyCount + ((allCount % everyCount != 0) ? 1 : 0); } /// <summary> /// 字典增加值 /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="dic"></param> /// <param name="key"></param> /// <param name="value"></param> public static void AddT2<T1, T2>(this Dictionary<T1, List<T2>> dic, T1 key, T2 value) { List<T2> list = dic.ContainsKey(key) ? dic[key] : new List<T2>(); if (!list.Contains(value)) { list.Add(value); } dic[key] = list; } } }
C#对象深拷贝DeepClone
#region 程序集 Kingdee.BOS, Version=7.7.2388.8, Culture=neutral, PublicKeyToken=null// D:\WorkSpace\K3Cloud\BIN\Kingdee.BOS.dll// ...
点击下载文档
本文2024-09-16 17:46:45发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-17881.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章