DotNet 笔记

东方欲晓,莫道君起早。

一个.Net反射帮助类

/*
 * 判断对象中是否有指定的属性(不区分大小写) JudgeHasProperty(string PropertyName, Object o)
 * 获取指定属性的值(不区分大小写)  GetPropertyValueByName(string PropertyName, Object o)
 * 遍历属性的名称/值(显示形式:name=value)  ForeachPropertyValues(Object o)
 * 实体对象相同属性名,自动赋值。 Assign<T, T1>(T left, T1 right, string[] attrs = null)
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Mvc.Util
{
    public class Reflection
    {
        #region 对基类Object对象 利用反射判断/获取属性值
        /// <summary>
        /// 判断 对象 中是否有该属性(不区分大小写)
        /// </summary>
        /// <param name="PropertyName">属性名称</param>
        /// <param name="o">目标对象</param>
        /// <returns></returns>
        public static bool JudgeHasProperty(string PropertyName, Object o)
        {
            if (o == null)
            {
                o = new { };
            }
            PropertyInfo[] p1 = o.GetType().GetProperties();
            bool b = false;
            foreach (PropertyInfo pi in p1)
            {
                if (pi.Name.ToLower() == PropertyName.ToLower())
                {
                    b = true;
                }
            }
            return b;
        }


        /// <summary>
        /// 获取指定属性的值(不区分大小写)
        /// </summary>
        /// <param name="PropertyName">属性名称</param>
        /// <param name="o">目标对象</param>
        /// <returns></returns>
        public static Object GetPropertyValueByName(string PropertyName, Object o)
        {
            if (o == null)
            {
                o = new { };
            }
            //创建一个返回对象
            Object returnObject = new Object();
            PropertyInfo[] p1 = o.GetType().GetProperties();
            foreach (PropertyInfo pi in p1)
            {
                if (pi.Name.ToLower() == PropertyName.ToLower())
                {
                    returnObject = pi.GetValue(o);
                }
            }
            return returnObject;
        }



        /// <summary>
        /// 遍历属性的名称/值(显示形式:name=value)
        /// </summary>
        /// <param name="o"></param>
        public static void ForeachPropertyValues(Object o)
        {
            if (o == null)
            {
                o = new { };
            }
            PropertyInfo[] p1 = o.GetType().GetProperties();
            foreach (PropertyInfo pi in p1)
            {
                Console.WriteLine(pi.Name + ":" + pi.GetValue(o, null));
            }

        }
        #endregion

        #region 用一个实体对另一个实体的相同属性赋值
        public static void Assign<T>(T left, T right, string[] attrs = null)
        {
            Type type = left.GetType();
            List<PropertyInfo> pList = type.GetProperties().ToList();
            for (int i = 0; i < pList.Count; i++)
            {
                //根据属性名获得指定的属性对象  
                PropertyInfo gc = type.GetProperty(pList[i].Name);
                //设置属性的值  
                if (attrs != null && attrs.Length > 0)
                {
                    string attrName = pList[i].Name.ToLower();
                    if (!attrs.Contains(attrName))
                    {
                        gc.SetValue(left, pList[i].GetValue(right, null), null);
                    }
                }
                else
                {
                    gc.SetValue(left, pList[i].GetValue(right, null), null);
                }

            }
        }

        /// <summary>
        /// 对象赋值
        /// </summary>
        /// <typeparam name="T">参数1类型</typeparam>
        /// <typeparam name="T1">参数2类型</typeparam>
        /// <param name="left">被赋值对象(等号左边的)</param>
        /// <param name="right">赋值对象(等号右边的)</param>
        /// <param name="attrs">忽略的字段</param>
        public static void Assign<T, T1>(T left, T1 right, string[] attrs = null)
        {
            Type type = left.GetType();
            List<PropertyInfo> pList = type.GetProperties().ToList();
            for (int i = 0; i < pList.Count; i++)
            {
                //根据属性名获得指定的属性对象
                PropertyInfo gc = type.GetProperty(pList[i].Name);
                //设置属性的值

                if (attrs != null && attrs.Length > 0)
                {
                    attrs = attrs.Select(r => r.ToLower()).ToArray();
                    string attrName = gc.Name.ToLower();
                    if (!attrs.Contains(attrName) && JudgeHasProperty(attrName, right))
                    {
                        gc.SetValue(left, GetPropertyValueByName(attrName, right), null);
                    }
                }
                else
                {
                    string attrName = gc.Name.ToLower();
                    if (JudgeHasProperty(attrName, right))
                    {
                        gc.SetValue(left, GetPropertyValueByName(attrName, right), null);
                    }
                }

            }
        }
        #endregion
    }



}

 

Loading