zoukankan      html  css  js  c++  java
  • 找到一段当初实现反射的代码

            /// <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; }

    名称:多罗贝勒
    博客地址:http://www.cnblogs.com/objctccc/
    欢迎转载

  • 相关阅读:
    哥哥牟:诺拉的死亡是由于寻找食物的粪便!
    Eclipse建筑物SSH(struts-2.2.3 + spring-2.5.6 + hibernate-3.6.8)相框-随着源代码
    Centos6.5下一个Ceph存储集群结构
    linux input如何固定设备event handler
    sizeof运营商
    【小言的设计模式】类之间的关系
    2015第11周五
    2015第11周四~代发公司招聘信息
    2015第11周三
    2015第11周二
  • 原文地址:https://www.cnblogs.com/objctccc/p/6115318.html
Copyright © 2011-2022 走看看