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
相關文章
相關標籤/搜索