using Newtonsoft.Json; using System.Reflection; namespace Znyc.CloudCar.Utility.Extensions { public class ConvertObjectExtensions { #region Method1 /// /// 将object对象转换为实体对象 /// /// 实体对象类名 /// object对象 /// private T ConvertObject(object asObject) where T : new() { //创建实体对象实例 var t = Activator.CreateInstance(); if (asObject != null) { Type type = asObject.GetType(); //遍历实体对象属性 foreach (var info in typeof(T).GetProperties()) { #pragma warning disable CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 object obj = null; #pragma warning restore CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 //取得object对象中此属性的值 var val = type.GetProperty(info.Name)?.GetValue(asObject); if (val != null) { //非泛型 if (!info.PropertyType.IsGenericType) obj = Convert.ChangeType(val, info.PropertyType); else//泛型Nullable<> { Type genericTypeDefinition = info.PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable<>)) { #pragma warning disable CS8604 // “object? Convert.ChangeType(object? value, Type conversionType)”中的形参“conversionType”可能传入 null 引用实参。 obj = Convert.ChangeType(val, Nullable.GetUnderlyingType(info.PropertyType)); #pragma warning restore CS8604 // “object? Convert.ChangeType(object? value, Type conversionType)”中的形参“conversionType”可能传入 null 引用实参。 } else { obj = Convert.ChangeType(val, info.PropertyType); } } info.SetValue(t, obj, null); } } } return t; } #endregion #region Method2 /// /// 将object对象转换为实体对象 /// /// 实体对象类名 /// object对象 /// public static T ConvertObjectByJson(object asObject) where T : new() { //将object对象转换为json字符 var json = JsonConvert.SerializeObject(asObject); //将json字符转换为实体对象 var t = JsonConvert.DeserializeObject(json); #pragma warning disable CS8603 // 可能返回 null 引用。 return t; #pragma warning restore CS8603 // 可能返回 null 引用。 } #endregion #region Method3 /// /// 将object尝试转为指定对象 /// /// /// public static T ConvertObjToModel(object data) where T : new() { if (data == null) return new T(); // 定义集合 T result = new T(); // 获得此模型的类型 Type type = typeof(T); string tempName = ""; // 获得此模型的公共属性 PropertyInfo[] propertys = result.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查object是否包含此列 // 判断此属性是否有Setter if (!pi.CanWrite) continue; try { object value = GetPropertyValue(data, tempName); if (value != DBNull.Value) { Type tempType = pi.PropertyType; pi.SetValue(result, GetDataByType(value, tempType), null); } } catch { } } return result; } /// /// 获取一个类指定的属性值 /// /// object对象 /// 属性名称 /// public static object GetPropertyValue(object info, string field) { #pragma warning disable CS8603 // 可能返回 null 引用。 if (info == null) return null; #pragma warning restore CS8603 // 可能返回 null 引用。 Type t = info.GetType(); IEnumerable property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; #pragma warning disable CS8603 // 可能返回 null 引用。 return property.First().GetValue(info, null); #pragma warning restore CS8603 // 可能返回 null 引用。 } /// /// 将数据转为制定类型 /// /// /// /// public static object GetDataByType(object data1, Type itype, params object[] myparams) { object result = new object(); try { if (itype == typeof(decimal)) { result = Convert.ToDecimal(data1); if (myparams.Length > 0) { result = Convert.ToDecimal(Math.Round(Convert.ToDecimal(data1), Convert.ToInt32(myparams[0]))); } } else if (itype == typeof(double)) { if (myparams.Length > 0) { result = Convert.ToDouble(Math.Round(Convert.ToDouble(data1), Convert.ToInt32(myparams[0]))); } else { result = double.Parse(Convert.ToDecimal(data1).ToString("0.00")); } } else if (itype == typeof(int)) { result = Convert.ToInt32(data1); } else if (itype == typeof(DateTime)) { result = Convert.ToDateTime(data1); } else if (itype == typeof(Guid)) { #pragma warning disable CS8604 // “Guid.Guid(string g)”中的形参“g”可能传入 null 引用实参。 result = new Guid(data1.ToString()); #pragma warning restore CS8604 // “Guid.Guid(string g)”中的形参“g”可能传入 null 引用实参。 } else if (itype == typeof(string)) { #pragma warning disable CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 result = data1.ToString(); #pragma warning restore CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 } } catch { if (itype == typeof(decimal)) { result = 0; } else if (itype == typeof(double)) { result = 0; } else if (itype == typeof(int)) { result = 0; } else if (itype == typeof(DateTime)) { #pragma warning disable CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 result = null; #pragma warning restore CS8600 // 将 null 文本或可能的 null 值转换为不可为 null 类型。 } else if (itype == typeof(Guid)) { result = Guid.Empty; } else if (itype == typeof(string)) { result = ""; } } #pragma warning disable CS8603 // 可能返回 null 引用。 return result; #pragma warning restore CS8603 // 可能返回 null 引用。 } #endregion } }