C# 反射,修改变量值(示例)

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sci
{
    public sealed class ManagerX
    {
        private static ArrayList BWH = new ArrayList();
        private static object BWh = new object();
        private bool BWI;
        private ClassX xObj;

        public ManagerX(string param)
        {
            xObj = ClassX.Instance();
        }
    }

    internal class ClassX
    {
        private string BWo;
        private bool TargetObj;

        private ClassX()
        {
            BWo = "";
            TargetObj = false;
        }

        public static ClassX Instance() { 
            return new ClassX();
        }
    }

    public class ReflectDemo
    {
        /// <summary>
        /// 反射修改示例(修改ManagerX.xObj.TargetObj = true)
        /// </summary>
        public static void Test()
        {
            var obj = new ManagerX("test");
            Enable(obj);
        }

        public static void Enable(Object obj)
        {
            Object xObj = GetDeclaredField(obj, "xObj");           // 获取内部变量 
            SetDeclaredField(xObj, "TargetObj", true);             // 修改内部变量值

            //Object TargetObj = GetDeclaredField(xObj, "TargetObj");
            //string result = TargetObj.ToString();
            //MessageBox.Show(result);
        }

        /// <summary>
        /// 获取obj下指定的变量
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        private static Object GetDeclaredField(Object obj, string fieldName)
        {
            FieldInfo field = obj.GetType().GetTypeInfo().GetDeclaredField(fieldName);
            return field.GetValue(obj);
        }

        /// <summary>
        /// 修改obj下指定变量值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static bool SetDeclaredField(Object obj, string fieldName, Object value)
        {
            try
            {
                FieldInfo field = obj.GetType().GetTypeInfo().GetDeclaredField(fieldName);
                object valueObj = Convert.ChangeType(value, field.FieldType);
                field.SetValue(obj, valueObj);
            }
            catch { return false; }
            return true;
        }

    }
}

其它相关功能:

cs 复制代码
        #region 反射获取和修改 sealed class 内部私有变量属性值

        /// <summary>
        /// 获取obj下指定的变量
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public Object GetDeclaredField(Object obj, string fieldName)
        {
            FieldInfo field = obj.GetType().GetTypeInfo().GetDeclaredField(fieldName);
            object valueObj = field.GetValue(obj);

            return valueObj;
        }

        /// <summary>
        /// 修改obj下指定变量值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetDeclaredField(Object obj, string fieldName, Object value)
        {
            try
            {
                FieldInfo field = obj.GetType().GetTypeInfo().GetDeclaredField(fieldName);

                Type type = field.FieldType;
                object valueObj = Convert.ChangeType(value, type);  // 转化为变量对应类型

                field.SetValue(obj, valueObj);                      // 设置值
            }
            catch { return false; }
            return true;
        }

        #endregion


        #region 获取类(非sealed class)内部变量

        /// <summary>
        /// 获取obj下指定的变量
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public Object GetField(Object obj, string fieldName)
        {
            //FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.GetField);

            FieldInfo field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic);

            Object valueObj = field.GetValue(obj);
            return valueObj;
        }

        /// <summary>
        /// 修改obj下指定变量值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetField(Object obj, string fieldName, Object value)
        {
            try
            {
                FieldInfo field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic);

                Type type = field.FieldType;
                object valueObj = Convert.ChangeType(value, type);  // 转化为变量对应类型

                field.SetValue(obj, valueObj);                      // 设置值
            }
            catch { return false; }
            return true;
        }

        #endregion
相关推荐
向宇it4 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
九鼎科技-Leo4 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
Heaphaestus,RC5 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
baivfhpwxf20235 小时前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#
直裾5 小时前
Scala全文单词统计
开发语言·c#·scala
ZwaterZ7 小时前
vue el-table表格点击某行触发事件&&操作栏点击和row-click冲突问题
前端·vue.js·elementui·c#·vue
ZwaterZ9 小时前
el-table-column自动生成序号&&在序号前插入图标
前端·javascript·c#·vue
SRC_BLUE_1712 小时前
SQLI LABS | Less-55 GET-Challenge-Union-14 Queries Allowed-Variation 2
oracle·c#·less
yngsqq12 小时前
037集——JoinEntities连接多段线polyline和圆弧arc(CAD—C#二次开发入门)
开发语言·c#·swift
Zԅ(¯ㅂ¯ԅ)12 小时前
C#桌面应用制作计算器进阶版01
开发语言·c#