你如何获得一类与反思所有属性和它的基类(最高层次)? (C#) [英] How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

查看:244
本文介绍了你如何获得一类与反思所有属性和它的基类(最高层次)? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,我现在是这样的:

So what I have right now is something like this:

PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);

其中, OBJ 是某个对象。

问题是一些我想要的属性是不是在 obj.GetType()他们在基类中的一个进一步向上。如果我停止调试器,并期待在obj,那么我也通过一些基地条目挖,看看我想要得到的属性。有没有我可以设置使其返回那些或我递归通过 Type.BaseType 层次挖掘和做的GetProperties

The problem is some of the properties I want aren't in obj.GetType() they're in one of the base classes further up. If I stop the debugger and look at obj, the I have to dig through a few "base" entries to see the properties I want to get at. Is there some binding flag I can set to have it return those or do I have to recursively dig through the Type.BaseType hierarchy and do GetProperties on all of them?

推荐答案

使用这样的:

PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

编辑:当然,正确的答案是,<一个href=\"http://stackoverflow.com/questions/245055/how-do-you-get-the-all-properties-of-a-class-and-its-base-classes-up-the-hierar#245131\">Jay. 的GetProperties()不带参数等同于的GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)。在 BindingFlags.FlattenHierarchy 在这里扮演的角色没有

Of course the correct answer is that of Jay. GetProperties() without parameters is equivalent to GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ). The BindingFlags.FlattenHierarchy plays no role here.

这篇关于你如何获得一类与反思所有属性和它的基类(最高层次)? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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