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
相关推荐
小码编匠36 分钟前
.NET 验证码生成神器基于 SkiaSharp 的高性能方案
后端·c#·.net
专注VB编程开发20年1 小时前
Aspose.words,Aspose.cells,vb.net,c#加载许可证,生成操作选择:嵌入的资源
c#·word·.net·vb.net
andy55201 小时前
.NET 使用 WMQ 连接Queue 发送 message 实例
xml·c#·wmq·c# 连接wmq·发送消息到wmq
破罐子不摔1 小时前
【C#使用S7.NET库读取和写入西门子PLC变量】
java·c#·.net
杰尼杰尼丶1 小时前
Winform MQTT客户端连接方式
c#·winform
weixin_307779132 小时前
C#实现HiveQL建表语句中特殊数据类型的包裹
开发语言·数据仓库·hive·c#
lixy5793 小时前
C# WPF 命令机制(关闭CanExecute自动触发,改手动)
c#·wpf
天地长久.3 小时前
C# N层架构和解耦
c#·解耦·多层架构
观无4 小时前
关于跨域与.NET的处理方案
c#
全栈小54 小时前
【C#】.net core 6.0 依赖注入常见问题之一,在构造函数使用的类,都需要注入到容器里,否则会提示如下报错,让DeepSeek找找原因,看看效果
c#·.netcore·依赖注入·deepseek