[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 ...
随机推荐
- Maven install报MojoFailureException
[ERROR] 位置: 类 com.spark.test.JavaDirectKafkaWordCount [ERROR] /I:/TrueTimeControlOnSparkByJava/src/m ...
- Cordova+Angularjs+Ionic 混合开发入门讲解
作为一名学习Android开发的学生,对于移动开发的发展趋势颇为关注,大家都知道,现在原生的移动开发在企业上基本很少使用了,大部分企业为了降低成本,选择了webapp,hybrid(混合开发)这两种模 ...
- azure 1元试用,如何创建虚拟机等
付了1元后,直接进 https://manage.windowsazure.cn 创建虚拟机即可.
- 【HTTP】使用 RestTemplete 实现 post请求
如上图,要求: post请求; x-www-form-urlencoded 类型; 如下代码没有进行整理,但是测试OK package com.chinamobile.epic.http; impor ...
- HTTP之Cookie
cookie是什么 浏览器存储在本地电脑上的一小段文本文件,cookie的存在主要是为了解决http协议无状态的问题,例如通过cookie来判断用户的登录状态,是否是某一个用户等. cookie的结构 ...
- 峰Redis学习(6)Redis 数据结构(sorted-set的操作)
第六节:Redis 数据结构之sorted-set 类型 存储Sorted-Set Sorted-Set和Set的区别 Sorted-Set中的成员在集合中的位置是有序的 存储Sorted-s ...
- 其他类想使用unittest的断言方法,就import unittest的框架,继承他,使用他里面的方法
在断言层 也可以同样用这个方法
- IDC:网管网
ylbtech-IDC:网管网 1.返回顶部 1. 中文名:网管网.网络管理信息 属 于:电信支撑网 通 过:工作站 网管网: 是接收.处理和传送网络管理信息的电信支撑网,它通过工作站.标 ...
- PG cannot execute UPDATE in a read-only transaction | How to add column if not exists on PostgreSQL
PG cannot execute UPDATE in a read-only transaction出现这种情况时,说明SQL语句可能是运行在一个PG集群中的非master节点上.查看data/pg ...
- [UE4]Authority,网络控制权
复制的条件 1.是否可复制开关打开 2.而且是服务器创建,或者放在关卡中. Authority,网络控制权 1.在网络游戏中,由当前进程创建的Actor,对其拥有网络控制权 2.Has Authori ...