public static partial class Extention { /// <summary> /// 构造函数 /// </summary> static Extention() { JsonSerializerSettings setting = new JsonSerializerSettings(); JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => { //日期类型默认格式化处理 setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; return setting; }); } /// <summary> /// 将一个object对象序列化,返回一个byte[] /// </summary> /// <param name="obj">能序列化的对象</param> /// <returns></returns> public static byte[] ToBytes(this object obj) { using (MemoryStream ms = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer(); } } /// <summary> /// 判断是否为Null或者空 /// </summary> /// <param name="obj">对象</param> /// <returns></returns> public static bool IsNullOrEmpty(this object obj) { if (obj == null) return true; else { string objStr = obj.ToString(); return string.IsNullOrEmpty(objStr); } } /// <summary> /// 将对象序列化成Json字符串 /// </summary> /// <param name="obj">需要序列化的对象</param> /// <returns></returns> public static string ToJson(this object obj) { var serializerSettings = new JsonSerializerSettings(); serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); return JsonConvert.SerializeObject(obj); } /// <summary> /// 实体类转json数据,速度快 /// </summary> /// <param name="t">实体类</param> /// <returns></returns> public static string EntityToJson(this object t) { if (t == null) return null; string jsonStr = ""; jsonStr += "{"; PropertyInfo[] infos = t.GetType().GetProperties(); for (int i = 0; i < infos.Length; i++) { jsonStr = jsonStr + """ + infos[i].Name + "":"" + infos[i].GetValue(t).ToString() + """; if (i != infos.Length - 1) jsonStr += ","; } jsonStr += "}"; return jsonStr; } /// <summary> /// 深复制 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="obj">对象</param> /// <returns></returns> public static T DeepClone<T>(this T obj) where T : class { if (obj == null) return null; return obj.ToJson().ToObject<T>(); } /// <summary> /// 改变实体类型 /// </summary> /// <param name="obj">对象</param> /// <param name="targetType">目标类型</param> /// <returns></returns> public static object ChangeType(this object obj, Type targetType) { return obj.ToJson().ToObject(targetType); } /// <summary> /// 改变实体类型 /// </summary> /// <typeparam name="T">目标泛型</typeparam> /// <param name="obj">对象</param> /// <returns></returns> public static T ChangeType<T>(this object obj) { return obj.ToJson().ToObject<T>(); } /// <summary> /// 初始化时间默认值 /// </summary> /// <param name="obj">对象</param> /// <returns></returns> public static T InitDateTime<T>(this T obj) where T : class { obj.GetType().GetProperties().ForEach(aProperty => { if (aProperty.PropertyType == typeof(DateTime)) { aProperty.SetValue(obj, DateTime.Now); } }); return obj; } /// <summary> /// 将对象序列化为XML字符串 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="obj">对象</param> /// <returns></returns> public static string ToXmlStr<T>(this T obj) { var jsonStr = obj.ToJson(); var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr); string xmlDocStr = xmlDoc.InnerXml; return xmlDocStr; } /// <summary> /// 将对象序列化为XML字符串 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="obj">对象</param> /// <param name="rootNodeName">根节点名(建议设为xml)</param> /// <returns></returns> public static string ToXmlStr<T>(this T obj, string rootNodeName) { var jsonStr = obj.ToJson(); var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr, rootNodeName); string xmlDocStr = xmlDoc.InnerXml; return xmlDocStr; } /// <summary> /// 获取某属性值 /// </summary> /// <param name="obj">对象</param> /// <param name="propertyName">属性名</param> /// <returns></returns> public static object GetPropertyValue(this object obj, string propertyName) { return obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).GetValue(obj); } /// <summary> /// 获取某属性值 /// </summary> /// <param name="obj">对象</param> /// <param name="propertyName">属性名</param> /// <param name="value">值</param> /// <returns></returns> public static void SetPropertyValue(this object obj, string propertyName, object value) { obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).SetValue(obj, value); } /// <summary> /// 获取枚举特性名 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string GetStateAttributeName(this object value) { Type type = value.GetType(); var field = type.GetField(value.ToString()); if (field != null) { Remark attribute = (Remark)field.GetCustomAttribute(typeof(Remark), true); return attribute.RemarkName; } else { return value.ToString(); } } /// <summary> /// 遍历类字段以及字段值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static DmModel getNameAndValue<T1, T2>(T1 t1, T2 t2) { System.Reflection.PropertyInfo[] properties1 = t1.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); DmModel dmModel = new DmModel(); foreach (System.Reflection.PropertyInfo item in properties1) { //t1属性值 var t1_value = GetPropertyValue(t1, item.Name); //t2属性值 var t2_value = GetPropertyValue(t2, item.Name); if (t1_value != t2_value) { dmModel.Key = item.Name; dmModel.Values = t2_value.ToString(); } } return dmModel; } }