• c#反射实现实体类生成以及数据获取与赋值


    public static IList<T> FillList<T>(System.Data.IDataReader reader)
    
        {
    
            IList<T> lst = new List<T>();
    
            while (reader.Read())
    
            {
    
                T RowInstance = Activator.CreateInstance<T>();
    
                foreach (PropertyInfo Property in typeof(T).GetProperties())
    
                {
    
                    foreach (
    
                        BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(
    
                        typeof(BindingFieldAttribute), true))
    
                    {
    
                        try
    
                        {
    
                            int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
    
                            if (reader.GetValue(Ordinal) != DBNull.Value)
    
                            {
    
                                Property.SetValue(RowInstance, 
    
                                    Convert.ChangeType(reader.GetValue(Ordinal), 
    
                                    Property.PropertyType), null);
    
                            }
    
                        }
    
                        catch
    
                        {
    
                            break;
    
                        }
    
                    }
    
                }
    
                lst.Add(RowInstance);
    
            }
    
            return lst;
    
        }
  • 相关阅读:
    JPA01
    mybatis入门
    PHP 循环- While循环
    PHP超级全局变量
    PHP 数组排序
    PHP数组
    PHP Switch语句
    PHP IF...Else语句
    PHP运算符
    PHP字符串变量
  • 原文地址:https://www.cnblogs.com/jeffrey77/p/2537748.html
Copyright © 2020-2023  润新知