C# 如何使用反射获取类及其基类(在层次结构中)的所有属性?(C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:39:19  来源:igfitidea点击:

How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

c#reflectiongetproperties

提问by Davy8

So what I have right now is something like this:

所以我现在拥有的是这样的:

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

where objis some object.

哪里obj有什么对象。

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.BaseTypehierarchy and do GetPropertieson all of them?

问题是我想要的一些属性不在obj.GetType()它们更远的基类之一中。如果我停止调试器并查看 obj,我必须挖掘一些“基本”条目以查看我想要获得的属性。是否有一些绑定标志我可以设置让它返回那些或者我必须递归地挖掘Type.BaseType层次结构并对GetProperties所有这些做?

采纳答案by Panos

Use this:

用这个:

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

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

编辑:当然,正确的答案是JayGetProperties()不带参数等价于GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static ). 在BindingFlags.FlattenHierarchy这里发挥任何作用。

回答by Seibar

If you access Type.BaseType, you can get the base type. You can recursively access each base type and you'll know when you've hit the bottom when your type is System.Object.

如果访问Type.BaseType,则可以获得基本类型。您可以递归访问每个基本类型,并且当您的类型为System.Object.

Type type = obj.GetType();
PropertyInfo[] info = type.GetProperties(BindingFlags.Public);
PropertyInfo[] baseProps = type.BaseType.GetProperties(BindingFlags.Public);

回答by Jay Bazuzi

I don't think it's that complicated.

我觉得没那么复杂。

If you remove the BindingFlagsparameter to GetProperties, I think you get the results you're looking for:

如果您删除BindingFlagsGetProperties的参数,我想您会得到您正在寻找的结果:

    class B
    {
        public int MyProperty { get; set; }
    }

    class C : B
    {
        public string MyProperty2 { get; set; }
    }

    static void Main(string[] args)
    {
        PropertyInfo[] info = new C().GetType().GetProperties();
        foreach (var pi in info)
        {
            Console.WriteLine(pi.Name);
        }
    }

produces

产生

    MyProperty2
    MyProperty

回答by Nicolas Cadilhac

Use:

用:

TypeDescriptor.GetProperties(obj);

回答by Marc Gravell

I would tend to agree with Nicolas; unless you know you need reflection, then ComponentModelis a viable alternative, with the advantage that you will get the correct metadata even for runtime models (such as DataView/DataRowView).

我倾向于同意尼古拉斯的观点;除非您知道需要反射,否则这ComponentModel是一个可行的替代方案,其优点是即使对于运行时模型(例如DataView/ DataRowView),您也可以获得正确的元数据。

For example:

例如:

    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(obj))
    {
        Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj));
    }

As an aside, you can also do some simple performance trickswith this; you can do the same with reflection and Delegate.CreateDelegate, but there is no centralised place to hide the logic away, unlike TypeDescriptorwith a TypeDescriptionProvider(don't worry if these are unfamiliar; you can just use the code "as is" ;-p).

顺便说一句,您还可以用它做一些简单的性能技巧;你可以用反射和做同样的事情Delegate.CreateDelegate,但是没有集中的地方来隐藏逻辑,不像TypeDescriptora TypeDescriptionProvider(如果这些不熟悉,不要担心;你可以“按原样”使用代码;-p)。