C# 对象操作

//*********************************************************************************
//**********************************修改记录****************************************
//修 改 人:
//修改时间:
//修改备注:
//*********************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace BasePoint.Common
{
    /// <summary>
    /// 类    名:EntityHelper
    /// 类 说 明:实体操作方法类
/// </summary>
    public static class EntityHelper
    {
        /// <summary>
        /// 方 法 名:DataRowToEntity
        /// 方法说明:转化数据行到实体对象
/// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dr">数据行</param>
        /// <param name="excludefields">不进行赋值的字段</param>
        /// <returns>实体对象</returns>
        public static T DataRowToEntity<T>(DataRow dr, string excludefields) where T : new()
        {
            Type t = typeof(T);
            PropertyInfo[] pinfos = t.GetProperties();
            T obj = new T();
            foreach (PropertyInfo pi in pinfos)
            { 
                if (!dr.Table.Columns.Contains(pi.Name))
                    continue;
                if (!string.IsNullOrWhiteSpace(excludefields) && excludefields.Contains(pi.Name))
                    continue;
                if (dr[pi.Name] != DBNull.Value)
                    pi.SetValue(obj, dr[pi.Name], null);
                else
                    pi.SetValue(obj, null, null);
            }
            return obj;
        }
        /// <summary>
        /// 方 法 名:DataRowToEntity
        /// 方法说明:转化数据行到实体对象
/// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="dr">The dr.</param>
        /// <returns></returns>
        public static T DataRowToEntity<T>(DataRow dr) where T : new()
        {
            return DataRowToEntity<T>(dr, string.Empty);
        }
        /// <summary>
        /// 方 法 名:DataTableToEntityList
        /// 方法说明:转化数据行到实体对象
/// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public static List<T> DataTableToEntityList<T>(DataTable dt) where T : new()
        {
            return DataTableToEntityList<T>(dt,null);
        }

        /// <summary>
        /// 方 法 名:DataTableToEntityList
        /// 方法说明:转化数据行到实体对象

        /// <typeparam name="T">实体类</typeparam>
        /// <param name="dt">数据表</param>
        /// <param name="excludefields">不进行赋值的字段</param>
        /// <returns></returns>
        public static List<T> DataTableToEntityList<T>(DataTable dt,string excludefields) where T : new()
        {
            List<T> list = new List<T>();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    T t = DataRowToEntity<T>(dr,excludefields);
                    if (t != null)
                        list.Add(t);
                }
            }
            return list;
        }
        /// <summary>
        /// 复制实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T CopyEntity<T>(T source) where T : new()
        {
            Type t = typeof(T);
            PropertyInfo[] pinfos = t.GetProperties();
            T obj = new T();
            foreach (PropertyInfo pi in pinfos)
            {
                object v = GetMemberValue(source,pi);
                pi.SetValue(obj, v, null);
            }
            return obj;
        }
        /// <summary>
        /// 判断两个实体是否一样
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <returns></returns>
        public static bool CheckEntityEquals(object source, object dest)
        {
            Type t = source.GetType();
            PropertyInfo[] pinfos = t.GetProperties();
            foreach (PropertyInfo pi in pinfos)
            {
                object v1 = GetMemberValue(dest, pi);
                object v = GetMemberValue(source, pi);
                if (!object.Equals(v1, v))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 方法说明:获取实体成员的值
    /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="member">实体成员信息</param>
        /// <returns>实体成员的值</returns>
        public static object GetMemberValue(object entity, MemberInfo member)
        {
            if ((entity == null) || (member == null))
            {
                return null;
            }

            object value = null;

            switch (member.MemberType)
            {
                case MemberTypes.Field:
                    FieldInfo field = member as FieldInfo;
                    if (field != null)
                    {
                        value = field.GetValue(entity);
                    }
                    break;
                case MemberTypes.Property:
                    PropertyInfo property = member as PropertyInfo;
                    if (property != null)
                    {
                        value = property.GetValue(entity, null);
                    }
                    break;
                default:
                    break;
            }

            return value;
        }
        /// <summary>
        /// 方法说明:获取实体成员的值
    /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="memberName">实体成员名称</param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <returns>实体成员的值</returns>
        public static object GetMemberValue(object entity, string memberName, bool ignoreCase)
        {
            if ((entity == null) || (string.IsNullOrEmpty(memberName)))
            {
                return null;
            }

            MemberInfo[] members;
            if (ignoreCase)
            {
                members = entity.GetType().GetMember(memberName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
            }
            else
            {
                members = entity.GetType().GetMember(memberName);
            }

            if ((members == null) || (members.Length == 0))
            {
                return null;
            }

            return GetMemberValue(entity, members[0]);
        }

        public static void SetMemberValue(object entity, string memberName, object memberValue)
        {
            if ((entity == null) || (string.IsNullOrEmpty(memberName)))
            {
                return;
            }

            MemberInfo[] members;
            members = entity.GetType().GetMember(memberName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if ((members == null) || (members.Length == 0))
            {
                return;
            }
            if (members[0] is FieldInfo)
            {
                if (memberValue == DBNull.Value)
                    (members[0] as FieldInfo).SetValue(entity, null);
                else
                    (members[0] as FieldInfo).SetValue(entity, memberValue);
            }
            else if (members[0] is PropertyInfo)
            {
                if (memberValue == DBNull.Value)
                    (members[0] as PropertyInfo).SetValue(entity, null, null);
                else
                    (members[0] as PropertyInfo).SetValue(entity, memberValue,null);
            }
        }
    }
}

 

C# 对象操作

上一篇:用Visual C#来清空回收站(1)


下一篇:[MSDN]最新Win7 SP1简体中文所有版本下载