From ac9fd706f9bb72bfeac27bac4bd0e3c45a0cfc7a Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 8 Jun 2021 10:54:51 +0800 Subject: [PATCH] add map special field method --- Models/SimApiBaseModel.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Models/SimApiBaseModel.cs b/Models/SimApiBaseModel.cs index 5223348..d3a077f 100644 --- a/Models/SimApiBaseModel.cs +++ b/Models/SimApiBaseModel.cs @@ -33,6 +33,30 @@ namespace SimApi.Models UpdateTime(); } + public void MapData(TS source, string[] mapFields) + { + //获取要赋值的源数据不为null的项目 + var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null) + .Select(x => new {x.Name, x.PropertyType}) + .ToDictionary(x => x.Name, x => x.PropertyType); + + //获取目标对象的属性信息 + var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType}) + .ToDictionary(x => x.Name, x => x.PropertyType); + foreach (var sp in sourceProps) + { + //检查源对象不为空的属性是否在目标对象中存在 + if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] && + mapFields.Contains(sp.Key)) + { + GetType().GetProperty(sp.Key)? + .SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source)); + } + } + + UpdateTime(); + } + /// /// 更新update时间 ///