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