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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

博客园 - 小寒

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