数据层反射对象

来源:互联网 发布:mac版梦幻西游更新失败 编辑:程序博客网 时间:2024/04/23 19:20
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Globalization;

namespace OSell.Utility.DBHelper
{
    /// <summary>
    /// Mapper
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class Mapper
    {
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> ToEntity<T>(DataTable dt)
        {
            try
            {
                List<T> list = new List<T>();
                if (dt != null)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        T entity = Activator.CreateInstance<T>();
                        PropertyInfo[] properties = entity.GetType().GetProperties();
                        foreach (PropertyInfo info in properties)
                        {
                            if (dt.Columns.Contains(info.Name) && info.CanWrite)
                            {
                                object objValue = row[info.Name];
                                if (objValue != DBNull.Value)
                                {
                                    if (info.PropertyType == typeof(DateTime?) || info.PropertyType == typeof(DateTime))
                                    {
                                        DateTime date = DateTime.MaxValue;
                                        DateTime.TryParse(objValue.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
                                        info.SetValue(entity, date, null);
                                    }
                                    else if (info.PropertyType == typeof(decimal?) || info.PropertyType == typeof(decimal))
                                    {
                                        Decimal de = 0M;
                                        Decimal.TryParse(objValue.ToString(), out de);
                                        info.SetValue(entity, de, null);
                                    }
                                    else
                                    {
                                        info.SetValue(entity, objValue, null);
                                    }
                                }
                                else
                                {
                                    info.SetValue(entity, null, null);
                                }
                            }
                        }
                        list.Add(entity);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static object ToEntity(DataRow adaptedrow, Type entitytype)
        {
            if (entitytype == null || adaptedrow == null)
            {
                return null;
            }
            object entity = Activator.CreateInstance(entitytype);
            CopyToEntity(entity, adaptedrow);
            return entity;
        }


        public static T ToEntity<T>(DataRow adaptedrow, T value) where T : new()
        {
            T item = new T();
            if (value == null || adaptedrow == null)
            {
                return item;
            }

            item = Activator.CreateInstance<T>();
            CopyToEntity(item, adaptedrow);
            return item;
        }

        public static void CopyToEntity(object entity, DataRow adaptedrow)
        {
            if (entity == null || adaptedrow == null)
            {
                return;
            }
            PropertyInfo[] propertyinfos = entity.GetType().GetProperties();
            foreach (PropertyInfo propertyinfo in propertyinfos)
            {
                if (!CansetPropertyValue(propertyinfo, adaptedrow))
                {
                    continue;
                }
                try
                {
                    if (adaptedrow[propertyinfo.Name] is DBNull)
                    {
                        propertyinfo.SetValue(entity, null, null);
                        continue;
                    }
                    SetPropertyValue(entity, adaptedrow, propertyinfo);
                }
                finally
                {

                }
            }
        }


        public static bool CansetPropertyValue(PropertyInfo propertyinfo, DataRow adaptedrow)
        {
            if (!propertyinfo.CanWrite)
            {
                return false;
            }

            if (!adaptedrow.Table.Columns.Contains(propertyinfo.Name))
            {
                return false;
            }
            return true;
        }


        public static void SetPropertyValue(object entity, DataRow adaptedrow, PropertyInfo propertyinfo)
        {
            if (propertyinfo.PropertyType == typeof(DateTime?) || propertyinfo.PropertyType == typeof(DateTime))
            {
                DateTime date = DateTime.MaxValue;
                DateTime.TryParse(adaptedrow[propertyinfo.Name].ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
                propertyinfo.SetValue(entity, date, null);
            }
            else
            {
                if (propertyinfo.PropertyType == typeof(decimal?) || propertyinfo.PropertyType == typeof(decimal))
                {
                    Decimal de = 0M;
                    Decimal.TryParse(adaptedrow[propertyinfo.Name].ToString(), out de);
                    propertyinfo.SetValue(entity, de, null);
                }
                else
                {
                    propertyinfo.SetValue(entity, adaptedrow[propertyinfo.Name], null);
                }
            }
        }
    }
}
0 0
原创粉丝点击