一、两个实体数据转换

/// <summary>
        /// 为属性赋值
        /// </summary>
        /// <typeparam name="T">源单类</typeparam>
        /// <typeparam name="S">需要转换的实体类</typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static S EntityConvert<T, S>(T source)
        {
            S target = Activator.CreateInstance<S>();
            var sType = source.GetType();
            var dType = typeof(S);
            foreach (PropertyInfo now in sType.GetProperties())
            {
                var name = dType.GetProperty(now.Name);
                if (name == null)
                    continue;
                dType.GetProperty(now.Name).SetValue(target, now.GetValue(source));
            }
            return target;
        }
View Code

相关文章:

  • 2021-06-01
  • 2021-06-15
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-21
  • 2022-01-17
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
相关资源
相似解决方案