通过反射获取类型的非公共属性 [英] Getting non-public properties of a type via reflection

查看:78
本文介绍了通过反射获取类型的非公共属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过反射获取类型的非公共属性?

How can I get non-public properties of a type via reflection?

推荐答案

是的,可以.在对 GetProperties()的调用中,指定 BindingFlags.NonPublic .

Yes, you can. Specify BindingFlags.NonPublic in your call to GetProperties().

class Program
{
    static void Main(string[] args)
    {
        var f = new Foo();
        foreach (var fi in f.GetType().GetProperties(
                               BindingFlags.NonPublic | BindingFlags.Instance))
        {
            Console.WriteLine(fi);
        }
    }       
}

public class Foo
{
    private string Prop { get; set; }
}

这篇关于通过反射获取类型的非公共属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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