[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 ...
随机推荐
- Linux下nohup日志输出过大问题解决方案
转载自:http://blog.csdn.net/shawnhu007/article/details/50971084 最近在一hadoop测试集群运行一个spark streaming程序,然后使 ...
- [转]bigbluebutton中文社区 / 开放API / bbb API
bigbluebutton中文社区 / 开放API / bbb API 创建会议 这个接口可以重复调用多次,而不会有副作用.这带来的好处就是能简化应用程序加会的流程,无论什么用户想要加会,都可以先创建 ...
- [转]Python中出错:ImportError: No module named win32com.client
Python中出错:ImportError: No module named win32com.client [问题] [已解决]Python中处理操作Excel中的图表(Chart,Graph) 的 ...
- SVN怎么触发Jenkins自动构建
通常,有几种方式可以在SVN仓库发生改变时触发Jenkins进行构建.第一种是,Jenkins主动轮询SVN仓库:第二种是,在SVN客户端(如TortoiseSVN)创建客户端hooks来触发构建:第 ...
- 关于namespace的使用
#include <string> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> int main ...
- 黄聪:ffmpeg参数说明(转载)
ffmpeg.exe -i F:\闪客之家\闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\11.flv ffmpeg -i F:\01.wm ...
- <亲测>阿里云centos7 挂载数据盘配置
阿里云centos7 挂载数据盘配置 2018年07月17日 15:13:53 阅读数:235更多 个人分类: linux阿里云ECS数据盘挂载 查看磁盘情况 fdisk -l 其中/dev/v ...
- vue 导出excel 多个sheet
npm install -save xlsx //下载依赖包 import Vue from 'vue'; import XLSX from 'xlsx'; /** * 导出数据报表xlsx文件 * ...
- SET NOCOUNT ON
每次我们在使用查询分析器调试SQL语句的时候,通常会看到一些信息,提醒我们当前有多少个行受到了影响,这是些什么信息?在我们调用的时候这些信息有用吗?是否可以关闭呢? 答案是这些信息在我们的客户端的应用 ...
- python学习疑问
1.(已解决) test = [1, 2, 3, 4] ", id(test)) def func(a): ", id(a)) a = a.remove(1) ", id ...