[C#]通过反射访问类私有成员
参考链接:
https://www.cnblogs.com/adodo1/p/4328198.html
代码如下:
using System;
using System.Reflection;
using UnityEngine; public static class ObjectExtension
{
//获取私有字段的值
public static T GetPrivateField<T>(this object instance, string fieldName)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo info = type.GetField(fieldName, flags);
return (T)info.GetValue(instance);
} //设置私有字段的值
public static void SetPrivateField(this object instance, string fieldName, object value)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo info = type.GetField(fieldName, flags);
info.SetValue(instance, value);
} //获取私有属性的值
public static T GetPrivateProperty<T>(this object instance, string propertyName)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo info = type.GetProperty(propertyName, flags);
return (T)info.GetValue(instance, null);
} //设置私有属性的值
public static void SetPrivateProperty<T>(this object instance, string propertyName, object value)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo info = type.GetProperty(propertyName, flags);
info.SetValue(instance, value, null);
} //调用私有方法
public static object CallPrivateMethod(this object instance, string methodName, params object[] param)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
MethodInfo info = type.GetMethod(methodName, flags);
return info.Invoke(instance, param);
}
} public class TestReflectionClass {
private int field;
private string Property { get; set; } public TestReflectionClass()
{
field = ;
Property = "a";
} private void SetValue(int field, string property)
{
this.field = field;
this.Property = property;
}
} public class TestReflection : MonoBehaviour { void Start ()
{
TestReflectionClass testClass = new TestReflectionClass(); print(testClass.GetPrivateField<int>("field"));//
print(testClass.GetPrivateProperty<string>("Property"));//a testClass.CallPrivateMethod("SetValue", , "b"); testClass.SetPrivateProperty<string>("Property", "c");
print(testClass.GetPrivateField<int>("field"));//
print(testClass.GetPrivateProperty<string>("Property"));//c
}
}
[C#]通过反射访问类私有成员的更多相关文章
- VC6.0中友元函数无法访问类私有成员的解决办法
举个例子: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #inclu ...
- java利用反射访问类的私有(private)属性及方法
Java语言中,在一个类中,为了不让外界访问到有的属性和方法,通常将其设置为private,用正常的方式(对象名.属性名,对象名.方法名)将无法访问此属性与方法,但有没有其他方法可以访问呢?答案是有的 ...
- java反射获取类的成员函数,成员变量,构造函数
package com.imooc.reflect;import javax.sound.midi.Soundbank;import java.lang.reflect.Constructor;imp ...
- [javaSE] 反射-获取类的成员属性和构造方法
成员属性和构造方法皆为对象,通过Class对象的方法可以得到 package com.tsh.reflect; import java.lang.reflect.Constructor; import ...
- Python基础(十二) 类私有成员和保护成员
python中的protected和private python中用 _var :变量名前一个下划线来定义,此变量为保护成员protected,只有类及其子类可以访问.此变量不能通过from XXX ...
- java 反射获取设置私有成员变量的值
for (Object arg:args) { //处理applicationCode Class<?> argClass = arg.getClass(); Field applicat ...
- C++之在类内部访问对象的私有成员
一.引言 今天看项目里的一段代码发现,竟然可以再类的成员函数中访问该类的对象的私有成员.感觉不可思议. 自己写的实例代码: #include <iostream> using namesp ...
- .net 反射访问私有变量和私有方法
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- C#中访问私有成员
首先访问一个类的私有成员不是什么好做法.大家都知道私有成员在外部是不能被访问的.一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员造访,可以套用下面这种非常好的方式去解决. pr ...
随机推荐
- Spring Boot 配置详解
Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...
- linux 内存映射-ioremap和mmap函数
最近开始学习Linux驱动程序,将内存映射和ioremap,mmap函数相关资料进行了整理 一,内存映射 对于提供了MMU(存储管理器,辅助操作系统进行内存管理,提供虚实地址转换等硬件支持)的处理器 ...
- 批量输出dwg文件中的文本
公司来了一批图纸,里面有一部分内容需要复制到excel中,几百张来图每一张都 手工复制,烦死了.编写一个CAD插件,自动导出文本,简单记录在下面. 想法是: 1.输入命令,选择所有dwg文件 2.挨个 ...
- STL基础--String
String 构造 string s1("Hello"); string s2("Hello", 3); //s2: Hel string s3(s1, 2); ...
- C++进阶--构造函数和析构函数中的虚函数
//############################################################################ /* 任何时候都不要在构造函数或析构函数中 ...
- Android 引用库项目,Debug 库项目
转自:http://www.cnblogs.com/xitang/p/3615768.html#commentform 使用引用项目,无法追到源代码,无法Debug库项目The JAR of this ...
- Concurrent包详解及使用场景
Concurrent包是jdk1.5所提供的一个针对高并发进行编程的包. 1.阻塞式队列 - BlockingQueue 遵循先进先出(FIFO)的原则.阻塞式队列本身使用的时候是需要指定界限的. 在 ...
- 堆叠箱子(基础dp)
P1086 时间限制: 1 Sec 内存限制: 128 MB提交: 38 解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 现有N种箱子,每种箱子高度H_i,数量C_i.现选取 ...
- vultr上 windows使用pptp拨号来实现冗余双网关的解决方案
rasdial是拨号程序,pptpvpn是网卡拨号名称,后面跟的是帐号和密码.pptpvpn见下图:就是提前创建好一个PPTP的拨号连接 上面是启动时候的计划任务,那么万一拨号中断,要继续重拨还需要做 ...
- [UE4]Reliable,可靠性
1.Reliable,不会丢失,立刻发出,适合重要的事件 2.Unreliable,可能会丢失,适合表现相关的和不重要的事件 3.全部的远程调用都使用Reliable,可能会造成网络拥堵 4.尽量避免 ...