zoukankan      html  css  js  c++  java
  • 常用方法 反射常见方法

         /// <summary>
            /// DataTable 转换为 对象List
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<T> DataTableToEntities<T>(this DataTable dt) where T : class, new()
            {
                if (null == dt || dt.Rows.Count == 0) { return null; }
                List<T> entities = new List<T>();
    
                foreach (DataRow row in dt.Rows)
                {
                    PropertyInfo[] pArray = typeof(T).GetProperties();
                    T entity = new T();
    
                    Array.ForEach<PropertyInfo>(pArray, p =>
                    {
                        object cellvalue = row[p.Name];
                        if (cellvalue != DBNull.Value)
                        {
                            //经过了几个版本的迭代,最后一个为最新的,摘自网上,已附原文地址
    
    
                            //4、原地址:https://blog.csdn.net/Simon1003/article/details/80839744
                            if (!p.PropertyType.IsGenericType)
                            {
                                p.SetValue(entity, Convert.ChangeType(cellvalue, p.PropertyType), null);
                            }
                            else
                            {
                                Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
                                if (genericTypeDefinition == typeof(Nullable<>))
                                {
                                    p.SetValue(entity, Convert.ChangeType(cellvalue, Nullable.GetUnderlyingType(p.PropertyType)), null);
                                }
                                else
                                {
                                    throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
                                }
                            }
    
    
                            //3、原地址:https://blog.csdn.net/hebbers/article/details/78957569
                            //Type type = p.PropertyType;
                            //if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                            //{
                            //    //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
                            //    System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                            //    //将type转换为nullable对的基础基元类型
                            //    type = nullableConverter.UnderlyingType;
                            //}
                            //p.SetValue(entity, Convert.ChangeType(cellvalue, type), null);
    
    
                            //2、自定义 这种很傻,但当前解决速度最快
                            //if (p.PropertyType.Name.Equals("Int32"))
                            //{
                            //    p.SetValue(entity, Convert.ToInt32(value), null);
                            //}
                            //else if (p.PropertyType.Name.Equals("String"))
                            //{
                            //    p.SetValue(entity, Convert.ToString(value), null);
                            //}
                            //else if (p.PropertyType.Name.Equals("Nullable`1"))
                            //{
                            //    p.SetValue(entity, Convert.ToInt32(value), null);
                            //}
                            ////其它类型 暂时不管 
    
    
                            //1、字段不为空可以用这种
                            //p.SetValue(entity, value, null);
                        }
                    });
                    entities.Add(entity);
                }
                return entities;
            }
    
            public static List<T> DataTableToEntities2<T>(this DataTable dt) where T : class, new()
            {
                if (null == dt || dt.Rows.Count == 0) { return null; }
                List<T> entities = new List<T>();
    
                foreach (DataRow row in dt.Rows)
                {
                    PropertyInfo[] pArray = typeof(T).GetProperties();
                    T entity = new T();
    
                    Array.ForEach<PropertyInfo>(pArray, p =>
                    {
                        object cellvalue = row[p.Name];
                        if (cellvalue != DBNull.Value)
                        {
                            if (!p.PropertyType.IsGenericType)
                            {
                                p.SetValue(entity, Convert.ChangeType(cellvalue, p.PropertyType), null);
                            }
                            else
                            {
                                Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
                                if (genericTypeDefinition == typeof(Nullable<>))
                                {
                                    p.SetValue(entity, Convert.ChangeType(cellvalue, Nullable.GetUnderlyingType(p.PropertyType)), null);
                                }
                                else
                                {
                                    throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
                                }
                            }
                        }
                    });
                    entities.Add(entity);
                }
                return entities;
            }
    
            public static List<T> DataTableToEntities3<T>(DataTable dt) where T : class, new()
            {
                if (null == dt || dt.Rows.Count == 0) { return null; }
                List<T> entities = new List<T>();
    
                foreach (DataRow row in dt.Rows)
                {
                    PropertyInfo[] pArray = typeof(T).GetProperties();
                    T entity = new T();
    
                    Array.ForEach<PropertyInfo>(pArray, p =>
                    {
                        object cellvalue = row[p.Name];
                        if (cellvalue != DBNull.Value)
                        {
                            //经过了几个版本的迭代,最后一个为最新的,摘自网上,已附原文地址
    
    
                            //4、原地址:https://blog.csdn.net/Simon1003/article/details/80839744
                            if (!p.PropertyType.IsGenericType)
                            {
                                p.SetValue(entity, Convert.ChangeType(cellvalue, p.PropertyType), null);
                            }
                            else
                            {
                                Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
                                if (genericTypeDefinition == typeof(Nullable<>))
                                {
                                    p.SetValue(entity, Convert.ChangeType(cellvalue, Nullable.GetUnderlyingType(p.PropertyType)), null);
                                }
                                else
                                {
                                    throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
                                }
                            }
    
    
                            //3、原地址:https://blog.csdn.net/hebbers/article/details/78957569
                            //Type type = p.PropertyType;
                            //if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                            //{
                            //    //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
                            //    System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(type);
                            //    //将type转换为nullable对的基础基元类型
                            //    type = nullableConverter.UnderlyingType;
                            //}
                            //p.SetValue(entity, Convert.ChangeType(cellvalue, type), null);
    
    
                            //2、自定义 这种很傻,但当前解决速度最快
                            //if (p.PropertyType.Name.Equals("Int32"))
                            //{
                            //    p.SetValue(entity, Convert.ToInt32(value), null);
                            //}
                            //else if (p.PropertyType.Name.Equals("String"))
                            //{
                            //    p.SetValue(entity, Convert.ToString(value), null);
                            //}
                            //else if (p.PropertyType.Name.Equals("Nullable`1"))
                            //{
                            //    p.SetValue(entity, Convert.ToInt32(value), null);
                            //}
                            ////其它类型 暂时不管 
    
    
                            //1、字段不为空可以用这种
                            //p.SetValue(entity, value, null);
                        }
                    });
                    entities.Add(entity);
                }
                return entities;
            }
    
            /// <summary>
            /// 跟属性赋值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="model"></param>
            /// <param name="name"></param>
            /// <param name="value"></param>
            public static void AssignValueToAttribute<T>(T model, string name, object value)
            {
                Type t = model.GetType();
                var p = t.GetProperty(name);
    
                if (!p.PropertyType.IsGenericType)
                {
                    p.SetValue(model, Convert.ChangeType(value, p.PropertyType), null);
                }
                else
                {
                    Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
                    if (genericTypeDefinition == typeof(Nullable<>))
                    {
                        p.SetValue(model, Convert.ChangeType(value, Nullable.GetUnderlyingType(p.PropertyType)), null);
                    }
                    else
                    {
                        throw new Exception("genericTypeDefinition != typeof(Nullable<>)");
                    }
                }
            }
    
    
            /// <summary>
            /// 获取属性值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="model"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public static object GetValueByAttribute<T>(T model, string name)
            {
                Type t = model.GetType();
                var p = t.GetProperty(name);
                return p.GetValue(model, null);
            }
    
            /// <summary>
            /// 跟属性赋值
            /// 把一个对象的属性 赋值到 另一个对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="model"></param>
            /// <param name="name"></param>
            /// <param name="model_old"></param>
            public static void AssignValueToAttribute<T>(T model, string name, T model_old)
            {
                Type t = model_old.GetType();
                var p = t.GetProperty(name);
                object obj = p.GetValue(model_old, null);
                
                AssignValueToAttribute(model, name, obj);
            }
  • 相关阅读:
    Cmake编译SDL2
    glog的使用
    win32下编译glog
    快速阅读《QT5.9 c++开发指南》1
    applyColorMap()研究(如果我对现有的colormap不满意,那么如何具体来做)
    如何判断轮廓是否为圆
    libopencv_shape.so.3.0: cannot open shared object file: No such file or directory 解决笔记
    OpenCV和RTSP的综合研究
    识别复杂的答题卡1(主要算法)
    识别简单的答题卡(Bubble sheet multiple choice scanner and test grader using OMR, Python and OpenCV——jsxyhelu重新整编)
  • 原文地址:https://www.cnblogs.com/guxingy/p/11362844.html
Copyright © 2011-2022 走看看