ObjectMapper 对象之间转换,针对两个对象属性名称不一样,这里用特性来关联
/// <summary> /// 序列化+反射转换(针对两个对象属性名称不一样,这里用特性来关联) /// </summary> /// <typeparam name="TObject">目标对象(增加DescriptionAttribute特性 来关联来源对象)</typeparam> /// <typeparam name="Source">来源对象</typeparam> /// <param name="t"></param> /// <param name="s"></param> /// <returns></returns> public static TObject SerializeByDescript<Source, TObject>(Source s) where TObject : class { Type type = typeof(TObject); TObject t = (TObject)Activator.CreateInstance(type); Dictionary<string, object> dic = new Dictionary<string, object>(); foreach (var property in type.GetProperties()) { var attributes = property.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { //有描叙特性,则用描叙特性代替属性 var descript = ((DescriptionAttribute)attributes[0]).Description;//获取描叙特性 var rproperty = typeof(Source).GetProperty(descript); if (property != null) { dic.Add(property.Name, rproperty.GetValue(s, null)); } } else { dic.Add(property.Name, property.GetValue(t, null)); } } return JsonConvert.DeserializeObject<TObject>(JsonConvert.SerializeObject(dic)); }
public class AnalysisDto { /// <summary> /// 指标类型 /// </summary> public int QualityIndexType { get; set; } /// <summary> /// 指标名称 /// </summary> public string QualityIndexName { get; set; } /// <summary> /// 当期人数 /// </summary> [Description("当期人数")] public string CurrentNum { get; set; } /// <summary> /// 当期指标人数 /// </summary> [Description("当期指标人数")] public string CurrentPeriodNum { get; set; } /// <summary> /// 当期指标率 /// </summary> [Description("当期指标率")] public string CurrentPeriodRatio { get; set; } /// <summary> /// 上期人数 /// </summary> [Description("上期人数")] public string LastNum { get; set; } /// <summary> /// 上期指标人数 /// </summary> [Description("上期指标人数")] public string LastPeriodNum { get; set; } /// <summary> /// 上期指标率 /// </summary> [Description("上期指标率")] public string LastPeriodRatio { get; set; } /// <summary> /// 环比 /// </summary> [Description("环比")] public string RingRatio { get; set; } /// <summary> /// 同期人数 /// </summary> [Description("同期人数")] public string CorrespondingNum { get; set; } /// <summary> /// 同期指标人数 /// </summary> [Description("同期指标人数")] public string CorrespondingPeriodNum { get; set; } /// <summary> /// 同期指标率 /// </summary> [Description("同期指标率")] public string CorrespondingPeriodRatio { get; set; } /// <summary> /// 同比 /// </summary> [Description("同比")] public string YearOnYearRatio { get; set; } /// <summary> /// 指标人数环比 /// </summary> [Description("指标人数环比")] public string TagRingRatio { get; set; } /// <summary> /// 总人数环比 /// </summary> [Description("总人数环比")] public string TotalRingRatio { get; set; } /// <summary> /// 指标人数同比 /// </summary> [Description("指标人数同比")] public string TagYearOnYearRatio { get; set; } /// <summary> /// 总人数同比 /// </summary> [Description("总人数同比")] public string TotalYearOnYearRatio { get; set; } }
public class AnalysisConvertDto { public string 当期人数 { get; set; } public string 当期指标人数 { get; set; } public string 当期指标率 { get; set; } public string 上期人数 { get; set; } public string 上期指标人数 { get; set; } public string 上期指标率 { get; set; } public string 环比 { get; set; } public string 同期人数 { get; set; } public string 同期指标人数 { get; set; } public string 同期指标率 { get; set; } public string 同比 { get; set; } public string 指标人数环比 { get; set; } public string 总人数环比 { get; set; } public string 指标人数同比 { get; set; } public string 总人数同比 { get; set; } }
var xmodel = await _xMapperRepository.QuerySingleOrDefaultAsync<AnalysisConvertDto>("GetSingleQualityIndex", ps); //接收存储过程返回的 中文实体对象数据 var model = JsonConvertHelper.SerializeByDescript<AnalysisConvertDto, AnalysisDto>(xmodel);//转换成数据库实体英文对象
到这里就已经实现了,两个对象之间 属性名称不一致的转换,这里主要通过自定义特性Description 来进行关联