源代码是别人的,你就不能修改源代码,只提供给你dll。或者你去维护别人的代码,源代码却有丢失。这样的情况如果你想知道私有成员的值,甚至去想直接调用类里面的私有方法。那怎么办呢?其实在.net中访问私有成员不是很难,这篇文章提供几个简单的方法让你如愿以偿。

为了让代码用起来优雅,使用扩展方法去实现。

1、得到私有字段的值:

public static T GetPrivateField<T>(this object instance, string fieldname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
return (T)field.GetValue(instance);
}

2、得到私有属性的值:

public static T GetPrivateProperty<T>(this object instance, string propertyname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
PropertyInfo field = type.GetProperty(propertyname, flag);
return (T)field.GetValue(instance, null);
}

3、设置私有成员的值:

public static void SetPrivateField(this object instance, string fieldname, object value) 

    BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; 
    Type type = instance.GetType(); 
    FieldInfo field = type.GetField(fieldname, flag); 
    field.SetValue(instance, value); 
}

4、设置私有属性的值: 
public static void SetPrivateProperty(this object instance, string propertyname, object value) 

    BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; 
    Type type = instance.GetType(); 
    PropertyInfo field = type.GetProperty(propertyname, flag); 
    field.SetValue(instance, value, null); 
}

5、调用私有方法:

public static T CallPrivateMethod<T>(this object instance, string name, params object[] param)
{
    BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
    Type type = instance.GetType();
    MethodInfo method = type.GetMethod(name, flag);
    return (T)method.Invoke(instance, param);
}

C#中访问私有成员技巧的更多相关文章

  1. C#中访问私有成员

    首先访问一个类的私有成员不是什么好做法.大家都知道私有成员在外部是不能被访问的.一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员造访,可以套用下面这种非常好的方式去解决. pr ...

  2. C#中访问私有成员--反射

    首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...

  3. 『无为则无心』Python面向对象 — 55、多层继承和继承中的私有成员

    目录 1.Python支持多层继承 (1)多层继承实现 (2)多层继承和多重继承区别 2.继承中的私有成员 (1)继承中父类私有属性和私有方法 (2)获取和修改私有属性值 1.Python支持多层继承 ...

  4. JavaScript中的私有成员[翻译]

    原作者:Douglas Crockford,原文地址:http://www.crockford.com/javascript/private.html JavaScript 是世界上被误解最深的编程语 ...

  5. C#为何不推荐在构造函数中访问虚成员

    如果在一个类中定义了虚属性或者虚方法,又在构造函数中访问了这个虚属性或方法,此时VisualStudio是不会给出警告,并且编译也没有问题,但是如果安装了Resharper插件则会给出警告提示:&qu ...

  6. C中的私有成员

    skynet_context声明在.h里 但定义在.c里面 外部使用的时候无法用ctx->handle获取私有成员,会提示解引用类型错误 必须用.h里函数获取ctx里属性.

  7. 使用C#反射机制访问类的私有成员【转】

    首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...

  8. 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 ...

  9. C++ //继承中的对象模型 //利用开发人员命令提示工具查看对象模型 //父类中所有非静态成员属性都会被 子类继承下去 //父类中私有成员属性 是被编译器给隐藏了 因此是访问不到 但是确实被继承下去了

    1 //继承方式 2 //语法:class 子类 :继承方式 父类 3 //继承方式 三种: 4 //1.公共继承 5 //2.保护继承 6 //3.私有继承 7 8 /* 9 #include &l ...

随机推荐

  1. Django REST framework基础:序列化

    表结构: class Article(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(m ...

  2. admin组件

    Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它 ...

  3. KERBEROS PROTOCOL TUTORIAL

    KERBEROS PROTOCOL TUTORIAL   This tutorial was written by Fulvio Ricciardi and is reprinted here wit ...

  4. Clion 配置

    plugins: one dark theme font : fira code retina

  5. (八)Index and Query a Document

    Let’s now put something into our customer index. We’ll index a simple customer document into the cus ...

  6. iOS开发基础篇-Button基础

    一.简单介绍  UIButton 的功能:响应用户操作.显示文字.显示图片.调整内部图片和文字的位置. 二. UIButton 的状态  UIControlStateNormal :普通状态,为默认情 ...

  7. PS外挂滤镜调出清晰对比照片

    最终效果 一.打开原图. 二.我们使用类似第一部分的相同方法,但设置上略有不同,我们将光线放在不同的地方.复制底层,执行滤镜-LUCIS ART水彩滤镜-LUCISART 选择 雕刻 设置参数为25. ...

  8. es6箭头函数 this 指向问题

    es5中 this 的指向 var factory = function(){ this.a = 'a'; this.b = 'b'; this.c = { a:'a+', b:function(){ ...

  9. Git各个状态之间转换指令总结

    基本状态标识 A- = untracked 未跟踪 A = tracked 已跟踪未修改 A+ = modified - 已修改未暂存 B = staged - 已暂存未提交 C = committe ...

  10. spring异常Unsatisfied dependency expressed through constructor parameter 0

    异常信息: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...