C#反射_两合并更新实体

        #region 更新实体模型
        /// <summary>
        /// 更新实体模型
        /// </summary>
        /// <typeparam name="T">源实体类型</typeparam>
        /// <typeparam name="S">最终合并后返回的实体类型</typeparam>
        /// <param name="tModel">源数据实体</param>
        /// <param name="outModel">最终合并后返回的实体</param>
        /// <returns>最终实体</returns>
        public static S EntityMerge<T, S>(T tModel, S outModel)
        {
            Type type = tModel.GetType();
            Type outType = outModel.GetType();
            var properties = type.GetProperties();
            var outProperties = outType.GetProperties();
            foreach (var property in properties)
            {
                foreach (var item in outProperties)
                {
                    if (property.Name == item.Name)
                    {
                        var value = property.GetValue(tModel);
                        item.SetValue(outModel, value, null);
                        break;
                    }
                }
            }
            return outModel;
        } 
        #endregion

 

posted @ 2019-05-27 11:52  小样儿哦  阅读(1271)  评论(0编辑  收藏  举报