首页 > 代码库 > 找到一段当初实现反射的代码

找到一段当初实现反射的代码

        /// <summary>
        /// 查询入口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static List<T> ModelFall<T>(string sql) where T : new()
        {
            //公共访问器
            CommonController cc = new CommonController();
            //得到的结果
            DataSet dataSet = cc.cmsMipService.ExecuteDataSet(sql);
            //进行反射
            return PutAllVal<T>(dataSet); ;
        }
        /// <summary>
        /// 遍历列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static List<T> PutAllVal<T>(DataSet ds) where T : new()
        {
            List<T> lists = new List<T>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    lists.Add(PutVal(new T(), row, ds.Tables[0]));
                }
            }
            return lists;
        }
        /// <summary>
        /// 根据特性反射
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="row"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public static T PutVal<T>(T entity, DataRow row, DataTable table) where T : new()
        {
            //初始化 如果为null
            if (entity == null)
            {
                entity = new T();
            }
            //得到类型
            Type type = typeof(T);
            //取得属性集合
            System.Reflection.PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo item in pi)
            {
                object[] keys = item.GetCustomAttributes(typeof(TimesProperty), true);
                string name = "";
                if (keys.Length > 0)
                {
                    TimesProperty a = new TimesProperty();
                    a = (TimesProperty)keys[0];
                    name = a.TableName;
                }
                else
                {
                    name = item.Name;
                }
                if (table.Columns.Contains(name))
                {
                    //给属性赋值
                    if (row[name] != null && row[name] != DBNull.Value)
                    {
                        if (item.PropertyType == typeof(System.Nullable<System.DateTime>))
                        {
                            item.SetValue(entity, Convert.ToDateTime(row[name].ToString()), null);
                        }
                        else
                        {
                            item.SetValue(entity, Convert.ChangeType(row[name], item.PropertyType), null);
                        }
                    }
                }
            }
            return entity;
        }

特性是这样写的。

自定义特性

    [Serializable]
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
    public class TimesProperty : System.Attribute
    {
        public virtual string TableName { get; set; }
    }

实体类里

[TimesProperty(TableName = "ID")]
public virtual long Id { get; set; }

找到一段当初实现反射的代码