鉴于这门课

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

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

我想找到我将用属性标记的私有物品_栏.可能吗?

我在属性中查找了属性,但从未查找过私有成员字段.

我需要设置什么绑定标志来获取私有字段?

推荐答案

使用BindingFlags.NonPublicBindingFlags.Instance标志

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

.net相关问答推荐

ZstdNet库的问题:Src大小不正确,异常

.NET模拟具有泛型返回类型的方法

dotnet ef dbcontext scaffold command --data-annotations 或 -d 命令行参数似乎不起作用

将 DataRowCollection 转换为 IEnumerable

不同命名空间中的部分类

使用 EPPlus 时如何设置列类型

实例化具有运行时确定类型的对象

如何删除只读文件?

.net 服务总线建议?

C# 方法可以定义为采用的最大参数数是多少?

List 和 IEnumerable 的实际区别

获取 .NET 程序集的 AssemblyInformationalVersion 值?

资源(.resx)文件有什么好处?

如何在 WPF 中的 Xaml 文件中添加注释?

String.Join 与 StringBuilder:哪个更快?

在 .NET 中获取默认打印机的最佳方法是什么

使用 C# vs F# 或 F# vs C# 有什么好处?

NServiceBus 与 MassTransit

如何在 ASP.NET MVC 中重定向到动态登录 URL

如何使用 XmlSerializer 将字符串序列化为 CDATA?