/// <summary> /// 数据行转换为目标对象 /// </summary> /// <typeparam name="T">目标类型</typeparam> /// <param name="dataRow">数据行</param> /// <returns>目标对象</returns> public static T ToObject <T>(this DataRow dataRow) { var result = ConvertManager.GetDefaultObject <T>(); if (result != null) { result.SetValueByDataRow(dataRow); } return(result == null ? default(T) : result); }
/// <summary> /// 属性复制 /// </summary> /// <typeparam name="T">复制的模型</typeparam> /// <param name="sourceM">复制源头对象</param> /// <param name="notCopyPropertyNames">不复制的属性名称</param> /// <returns>复制的对象</returns> public static T CopyProperties <T>(this object sourceM, params string[] notCopyPropertyNames) { if (sourceM == null) { return(default(T)); } var targetM = ConvertManager.GetDefaultObject <T>(); sourceM.CopyProperties(targetM, notCopyPropertyNames); return(targetM); }
/// <summary> /// 属性复制 /// </summary> /// <typeparam name="T">复制的模型</typeparam> /// <param name="sourceM">复制源头对象</param> /// <param name="isCopy">是否复制</param> /// <returns>复制的对象</returns> public static T CopyProperties <T>(this object sourceM, Func <PropertyInfo, object, bool> isCopy) { if (sourceM == null) { return(default(T)); } var targetM = ConvertManager.GetDefaultObject <T>(); sourceM.CopyProperties(targetM, isCopy); return(targetM); }
/// <summary> /// Json字符串转换对象 /// </summary> /// <typeparam name="T">目标对象类型</typeparam> /// <param name="jsonStr">Json字符串</param> /// <returns>转换后的对象</returns> public static T JsonToObject <T>(this string jsonStr) { try { var model = ConvertManager.GetDefaultObject <T>(); JsonConvert.PopulateObject(jsonStr, model); return(model); } catch (Exception ex) { throw new MateralConvertException("Json字符串有误", ex); } }
/// <summary> /// 获得默认对象 /// </summary> /// <typeparam name="T">要设置的类型</typeparam> /// <param name="obj">要设置的对象</param> /// <returns>默认对象</returns> public static T GetDefaultObject <T>(this object obj) { return(ConvertManager.GetDefaultObject <T>()); }
/// <summary> /// 获得默认对象 /// </summary> /// <param name="obj">要设置的对象</param> /// <param name="type">要设置的类型</param> /// <returns>默认对象</returns> public static object GetDefaultObject(this object obj, Type type) { return(ConvertManager.GetDefaultObject(type)); }