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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 小寒

Postman or TestNG for automated testing adb.exe 已停止工作 解决 用户中心 - 博客园 移动UI设计 c3p0数据源的使用初步及Mysql8小时问题解决 npm+node+cordova+ionic 版本匹配 ngrok - 小寒 vi command speech recognition resource mysql Workbench 执行删除命令 备份Mysql数据库BAT脚本 C# double 四舍五入 mysql CREATE USER 自定义属性 Cordova Ionic AngularJS centos mariadb Nginx 负载均衡 使用Aspose.Cells生成Excel的方法详解(转)
ConvertHelper 通用类
小寒 · 2015-12-29 · via 博客园 - 小寒
public class ConvertHelper<T> where T : new()
    {
        private static Dictionary<Type, List<IPropertyConvertInfo>> ConvertInfo = new Dictionary<Type, List<IPropertyConvertInfo>>();

        public static ObservableCollection<T> ConvertToList(DataTable dt)
        {
            List<IPropertyConvertInfo> convertInfo = ConvertInfo.ContainsKey(typeof(T)) ? ConvertInfo[typeof(T)] : ParseConvertInfo(typeof(T));
            convertInfo = convertInfo.Where(v => dt.Columns.Contains(v.ColumnName)).ToList();
            return new ObservableCollection<T>(dt.Rows.Cast<DataRow>().Select(v => ConvertRowToModel(convertInfo, v)));
        }

        #region ==== Private Mothods ====

        private static List<IPropertyConvertInfo> ParseConvertInfo(Type modelType)
        {
            var properties = modelType.GetProperties().Where(v => v.IsDefined(typeof(ColumnNameAttribute), true))
                .Select(v => CreatePropertyConvertInfo(v)).ToList();
            ConvertInfo[modelType] = properties;
            return properties;
        }

        private static IPropertyConvertInfo CreatePropertyConvertInfo(PropertyInfo property)
        {
            IPropertyConvertInfo info = Activator.CreateInstance(typeof(PropertyConvertInfo<>).MakeGenericType(property.PropertyType)) as IPropertyConvertInfo;
            info.ColumnName = ((ColumnNameAttribute)property.GetCustomAttributes(typeof(ColumnNameAttribute), true)[0]).ColumnName;
            info.Property = property;
            return info;
        }

        private static T ConvertRowToModel(List<IPropertyConvertInfo> convertInfo, DataRow dataRow)
        {
            T model = new T();
            convertInfo.ForEach(v => v.DoFetchData(model, dataRow));
            return model;
        }

        #endregion ==== Private Mothods ====
    }

    internal interface IPropertyConvertInfo
    {
        PropertyInfo Property { get; set; }
        string ColumnName { get; set; }
        void DoFetchData(object model, DataRow row);
    }
    internal class PropertyConvertInfo<TProperty> : IPropertyConvertInfo
    {
        public PropertyInfo Property { get; set; }
        public string ColumnName { get; set; }

        public void DoFetchData(object model, DataRow row)
        {
            if (!(row[ColumnName] is DBNull))
            {
                if (row[ColumnName].GetType().ToString() == "System.Decimal")
                {
                    object value = Convert.ToDouble(row[ColumnName]);
                    Property.SetValue(model, value, null);
                }
                else if (row[ColumnName].GetType().ToString() =="System.SByte")
                {
                    object value = Convert.ToBoolean(row[ColumnName]);
                    Property.SetValue(model, value, null);
                }
                else
                {
                    Property.SetValue(model, row.Field<TProperty>(ColumnName), null);
                }
            }
        }
    }