• 反射操作辅助类ReflectionUtil


    这篇文章的目的是介绍这样一种方式,就是在写一个函数的时候,传递的参数是object类型的,在这个函数里面想访问这个参数对象的某一属性值,我们知道这个属性值的name,但是一般情况下,object对象是没法获取具体属性的值的,所以用下面的方式可以获取。此文章为转载,原文在:http://lsyyxcn.blog.163.com/blog/static/22740531201002792629559/

    /// <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("
    ");
                }
    
                return strBuilder.ToString();
            }
        }
    
    反射操作辅助类ReflectionUtil测试代码:
        public class TestReflectionUtil
        {
            public static string Execute()
            {
                string result = string.Empty;
                result += "使用ReflectionUtil反射操作辅助类:" + "
    ";
    
                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}!
     
    ", ex.Message);
                }
                return result;
            }
    
            public static string DecriptPerson(Person person)
            {
                string result = string.Empty;
    
                result += "name:" + ReflectionUtil.GetField(person, "name") + "
    ";
                result += "age" + ReflectionUtil.GetField(person, "age") + "
    ";
    
                result += "Name:" + ReflectionUtil.GetProperty(person, "Name") + "
    ";
                result += "Age:" + ReflectionUtil.GetProperty(person, "Age") + "
    ";
    
                result += "Say:" + ReflectionUtil.InvokeMethod(person, "Say", new object[] {}) + "
    ";
    
                result += "操作完成!
     
    ";
    
                return result;
            }
        }

    /// <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(" ");
                }

                return strBuilder.ToString();
            }
        }

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

                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}! ", ex.Message);
                }
                return result;
            }

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

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

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

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

                result += "操作完成! ";

                return result;
            }
        }

  • 相关阅读:
    C++ *this与this的区别(系个人转载,个人再添加相关内容)
    C++ 友元(系转载多人博客,添加个人见解)
    C++模板详解(系转载,但是个人添加了一些内容)
    实验三:klee的执行重现机制(示例分析)
    klee错误汇报二:KLEE的optimize选项的一个困惑
    KLEE错误汇报一:One phenomenon after the execution using klee
    实验一:使用符号执行工具klee对软件进行破解(来自于klee官网)
    个人发现的createProcess调用漏洞
    docker运行时设置redis密码并替换redis默认的dump.rdb
    saltstack入门个人笔记
  • 原文地址:https://www.cnblogs.com/jin-/p/5038022.html
Copyright © 2020-2023  润新知