反射操作辅助类ReflectionUtil

/// <summary>
    /// 反射操作辅助类
    /// </summary>
    public sealed class ReflectionUtil
    {
        private ReflectionUtil()
        {
        }

        private static BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                   BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

        /** <summary>
        /// 执行某个方法
        /// </summary>
        /// <param name="obj">指定的对象</param>
        /// <param name="methodName">对象方法名称</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static object InvokeMethod(object obj, string methodName, object[] args)
        {
            object objResult = null;
            Type type = obj.GetType();
            objResult = type.InvokeMember(methodName, bindingFlags | BindingFlags.InvokeMethod, null, obj, args);
            return objResult;
        }

        /** <summary>
        /// 设置对象字段的值
        /// </summary>
        public static void SetField(object obj, string name, object value)
        {
            FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags);
            object objValue = Convert.ChangeType(value, fieldInfo.FieldType);
            fieldInfo.SetValue(objValue, value);
        }

        /** <summary>
        /// 获取对象字段的值
        /// </summary>
        public static object GetField(object obj, string name)
        {
            FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags);
            return fieldInfo.GetValue(obj);
        }

        /** <summary>
        /// 设置对象属性的值
        /// </summary>
        public static void SetProperty(object obj, string name, object value)
        {
            PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags);
            object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
            propertyInfo.SetValue(obj, objValue, null);
        }

        /** <summary>
        /// 获取对象属性的值
        /// </summary>
        public static object GetProperty(object obj, string name)
        {
            PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags);
            return propertyInfo.GetValue(obj, null);
        }

        /** <summary>
        /// 获取对象属性信息(组装成字符串输出)
        /// </summary>
        public static string GetProperties(object obj)
        {
            StringBuilder strBuilder = new StringBuilder();
            PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bindingFlags);

            foreach (PropertyInfo property in propertyInfos)
            {
                strBuilder.Append(property.Name);
                strBuilder.Append(":");
                strBuilder.Append(property.GetValue(obj, null));
                strBuilder.Append("/r/n");
            }

            return strBuilder.ToString();
        }
    }

反射操作辅助类ReflectionUtil测试代码:
    public class TestReflectionUtil
    {
        public static string Execute()
        {
            string result = string.Empty;
            result += "使用ReflectionUtil反射操作辅助类:" + "/r/n";

            try
            {
                Person person = new Person();
                person.Name = "wuhuacong";
                person.Age = 20;
                result += DecriptPerson(person);

                person.Name = "Wade Wu";
                person.Age = 99;
                result += DecriptPerson(person);
            }
            catch (Exception ex)
            {
                result += string.Format("发生错误:{0}!/r/n /r/n", ex.Message);
            }
            return result;
        }

        public static string DecriptPerson(Person person)
        {
            string result = string.Empty;

            result += "name:" + ReflectionUtil.GetField(person, "name") + "/r/n";
            result += "age" + ReflectionUtil.GetField(person, "age") + "/r/n";

            result += "Name:" + ReflectionUtil.GetProperty(person, "Name") + "/r/n";
            result += "Age:" + ReflectionUtil.GetProperty(person, "Age") + "/r/n";

            result += "Say:" + ReflectionUtil.InvokeMethod(person, "Say", new object[] {}) + "/r/n";

            result += "操作完成!/r/n /r/n";

            return result;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值