zoukankan      html  css  js  c++  java
  • 反射ModelToDto

    public class ModelToDTO
        {
            /// <summary>
            /// 根据模型对象获取扁平类对象
            /// </summary>
            /// <typeparam name="S">模型类</typeparam>
            /// <typeparam name="T">传输类</typeparam>
            /// <param name="s">模型类对象</param>
            /// <param name="t">传输类对象</param>
            /// <returns></returns>
            public static T GetDTOModel<S, T>(S s, T t) {
                if (s != null)
                {
                    AutoMapping<S, T>(s, t);
                    return t;
                }
                else {
                    return default(T);
                }
            }
            
            /// <summary>
            /// 实体属性反射
            /// </summary>
            /// <typeparam name="S">赋值对象</typeparam>
            /// <typeparam name="T">被赋值对象</typeparam>
            /// <param name="s"></param>
            /// <param name="t"></param>
            private static void AutoMapping<S, T>(S s, T t)
            {
                PropertyInfo[] pps = GetPropertyInfos(s.GetType());
                Type target = t.GetType();
    
                foreach (var pp in pps)
                {
                    PropertyInfo targetPP = target.GetProperty(pp.Name);
                    object value = pp.GetValue(s, null);
    
                    if (targetPP != null && value != null)
                    {
                        targetPP.SetValue(t, value, null);
                    }
                }
            }
    
            //获取对象属性
            private static PropertyInfo[] GetPropertyInfos(Type type)
            {
                return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
        }
    

      

  • 相关阅读:
    css flex布局实现后台页面
    html5 css iframe实现后台框架,仅用于学习案例
    nginx 多个网站配置
    nginx 负载 访问时 去掉端口
    nginx 负载
    解标准数独算法
    C++ execute linux cmd and retrieve the output
    C++ generate in Ubuntu
    shell操作典型案例--FTP操作
    PHP7 新写法
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088179.html
Copyright © 2011-2022 走看看