码迷,mamicode.com
首页 > Windows程序 > 详细

C# DataRow的扩展

时间:2016-06-07 19:12:36      阅读:614      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
    /// 对于entity的一些常用的扩展方法
    /// </summary>
    public static class MyExtension
    {
        /// <summary>
        /// 把DataRow转换成一个指定的实体类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T ToEntity<T>(this DataRow dr)
        {
            var type = typeof (T);
          
            var ins = Activator.CreateInstance<T>();
            var pis = type.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                var myProType = pi.PropertyType.Name.ToLower();
                var colName = pi.Name;
                object val = String.Empty;
                var myAttr = pi.GetCustomAttributes<PropertyTypeAttribute>().FirstOrDefault();
                if (myAttr != null)
                {
                    if (!String.IsNullOrEmpty(myAttr.MyColName))
                    {
                        colName = myAttr.MyColName;
                    }
                    if (myAttr.MyType!=null)
                    {
                        myProType = myAttr.MyType.Name.ToLower();
                    }
                }
                if (!dr.Table.Columns.Contains(colName))
                {
                    continue;
                }
                switch (myProType)
                {
                    case "string"://String
                        val = dr[colName] == DBNull.Value ? String.Empty : dr[colName].ToString();
                        break;
                    case "int32"://int
                        val = dr[colName] == DBNull.Value ? 0 : Int32.Parse(dr[colName].ToString());
                        break;
                    case "int64"://long
                        val = dr[colName] == DBNull.Value ? 0 : Int64.Parse(dr[colName].ToString());
                        break;
                    case "decimal"://Decimal
                        val = dr[colName] == DBNull.Value ? Decimal.Zero : Decimal.Parse(dr[colName].ToString());
                        break;
                    case "double"://Double
                        val = dr[colName] == DBNull.Value ? 0 : Double.Parse(dr[colName].ToString());
                        break;
                    case "boolean"://Boolean
                        val = dr[colName] != DBNull.Value && Boolean.Parse(dr[colName].ToString());
                        break;
                    case "datetime"://DateTime
                        val = dr[colName] != DBNull.Value ? DateTime.Parse("1970-01-01 00:00:00") : DateTime.Parse(dr[colName].ToString());
                        break;
                    default:
                        break;
                }
                if (pi.CanWrite)
                {
                    pi.SetValue(ins, val);
                }
            }
            return ins;
        }

C# DataRow的扩展

标签:

原文地址:http://www.cnblogs.com/xiaozhuaweiliang/p/5568024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!