使用 Reflection 查找私有字段? [英] Find a private field with Reflection?

查看:21
本文介绍了使用 Reflection 查找私有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这个类

class Foo
{
    // Want to find _bar with reflection
    [SomeAttribute]
    private string _bar;

    public string BigBar
    {
        get { return this._bar; }
    }
}

我想找到我将用属性标记的私有项 _bar.那可能吗?

I want to find the private item _bar that I will mark with a attribute. Is that possible?

我已经使用我寻找属性的属性完成了这项工作,但从来没有寻找私有成员字段.

I have done this with properties where I have looked for an attribute, but never a private member field.

获取私有字段需要设置哪些绑定标志?

What are the binding flags that I need to set to get the private fields?

推荐答案

使用 BindingFlags.NonPublicBindingFlags.Instance 标志

FieldInfo[] fields = myType.GetFields(
                         BindingFlags.NonPublic | 
                         BindingFlags.Instance);

这篇关于使用 Reflection 查找私有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆