惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Vercel News
Vercel News
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
Jina AI
Jina AI
Y
Y Combinator Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI

博客园 - 多罗贝勒

北京西服定制排行榜,西服定制店铺推荐 微信公众号开发-工具 PHP 环境搭建工具 host1plus 如何安装 OpenVPN - 多罗贝勒 .NET CORE MVC网站体验 111 【微信小程序】认识微信小程序 Web api 测试 工具WebApiTestClient C# 线程同步 C# 线程基础 WCF概念 C#知识点总结【2】 C#知识点总结【1】 【2016-11-28日面试总结】 .NET 面试基本技术整理 .NET 面试题整理 【DNN】C#中类的一些特性标注 【DNN 系列 创建WEB模块 项目】 SharePoint 使用Expression Web 设计网站 【DNN】 安装问题 【DNN】 制作一个扩展程序
找到一段当初实现反射的代码
多罗贝勒 · 2016-11-29 · via 博客园 - 多罗贝勒
        /// <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; }