C# 在 .NET 反射中使用 GetProperties() 和 BindingFlags.DeclaredOnly
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1544979/
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
Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection
提问by UpTheCreek
If I use
如果我使用
sometype.GetProperties();
I get all of the properties from the type and it's parent. However I only want to retrieve the properties defined explicitly in this type (not the parents). I thought that was what the BindingFlags.DeclaredOnly
option was for.
我从类型和它的父级中获取所有属性。但是,我只想检索在此类型(而不是父项)中明确定义的属性。我认为这就是BindingFlags.DeclaredOnly
选项的用途。
However, when I try this:
但是,当我尝试这样做时:
sometype.GetProperties(BindingFlags.DeclaredOnly);
I get 0 properties.
我得到 0 个属性。
Anyone know what I am doing wrong?
有谁知道我做错了什么?
采纳答案by Pete
If you specify any BindingFlags
, then you need to specify explicitly what properties you want to get. For example:
如果指定 any BindingFlags
,则需要明确指定要获取的属性。例如:
sometype.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
回答by Groo
To summarize:
总结一下:
if you use the
GetProperties()
overload (without parameters), you will get all public properties.on the other hand, if you use the
GetProperties(BindingFlags)
overload (the one which accepts aBindingFlags
parameter), then you need to specify a bitwise OR of at least one from each group of the following flags:BindingFlags.Instance
/BindingFlags.Static
(instance vs static properties),BindingFlags.Public
/BindingFlags.NonPublic
(public vs non-public properties).
如果您使用
GetProperties()
重载(不带参数),您将获得所有公共属性。另一方面,如果您使用
GetProperties(BindingFlags)
重载(接受BindingFlags
参数的重载),那么您需要指定以下每组标志中至少一个的按位或:BindingFlags.Instance
/BindingFlags.Static
(实例与静态属性),BindingFlags.Public
/BindingFlags.NonPublic
(公共与非公共属性)。
For example, to get public static properties, you will need to call GetProperties(BindingFlags.Public | BindingFlags.Static)
in order to get results. Calling only GetProperties(BindingFlags.Public)
or GetProperties(BindingFlags.Static)
won't return any results.
例如,要获取公共静态属性,您需要调用GetProperties(BindingFlags.Public | BindingFlags.Static)
才能获取结果。只调用GetProperties(BindingFlags.Public)
或GetProperties(BindingFlags.Static)
不会返回任何结果。
Note also that specifying BindingFlags.Default
will return an empty array.
另请注意,指定BindingFlags.Default
将返回一个空数组。
Full details can be found in MSDN documentation for GetProperties(BindingFlags):
可以在GetProperties(BindingFlags) 的 MSDN 文档中找到完整的详细信息:
The following BindingFlags filterflags can be used to define which nested types to include in the search:
- You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.- Specify
BindingFlags.Public
to include public properties in the search.- Specify
BindingFlags.NonPublic
to include non-public methods (that is, private, internal, and protected methods) in the search. Only protected and internal methods on base classes are returned; private methods on base classes are not returned.- Specify
BindingFlags.FlattenHierarchy
to include public and protected static members up the hierarchy; private static members in inherited classes are not included.The following BindingFlags modifierflags can be used to change how the search works:
BindingFlags.DeclaredOnly
to search only the properties declared on the Type, not properties that were simply inherited.
以下 BindingFlags过滤器标志可用于定义要包含在搜索中的嵌套类型:
- 您必须指定
BindingFlags.Instance
或BindingFlags.Static
才能获得回报。- 指定
BindingFlags.Public
在搜索中包括公共属性。- 指定
BindingFlags.NonPublic
在搜索中包括非公共方法(即私有、内部和受保护的方法)。只返回基类的受保护和内部方法;不返回基类上的私有方法。- 指定
BindingFlags.FlattenHierarchy
在层次结构中包含公共和受保护的静态成员;不包括继承类中的私有静态成员。以下 BindingFlags修饰符标志可用于更改搜索的工作方式:
BindingFlags.DeclaredOnly
仅搜索在 Type 上声明的属性,而不是简单继承的属性。
回答by JaredPar
You need to expand your BindingsFlag a bit. They need to at least include what accessibility level and instance vs. static in order to get anything meaningful back.
您需要稍微扩展一下 BindingsFlag。他们至少需要包括什么可访问性级别和实例与静态,以便获得任何有意义的回报。
I think what you are actually looking for is the following
我认为您实际要寻找的是以下内容
var flags = BindingFlags.DeclaredOnly
| BindingFlags.Instance
| BindingFlags.Public;
someType.GetProperties(flags);
回答by Rohit Vipin Mathews
From the MSDNsite.
来自MSDN站点。
Default (member) Specifies no binding flag.
You must specify Instance or Static along with Public or NonPublic or no members will be returned.
默认(成员)指定无绑定标志。
您必须指定 Instance 或 Static 以及 Public 或 NonPublic 否则不会返回任何成员。
Hence unless you specify the binding flags you get nothing.
因此,除非您指定绑定标志,否则您将一无所获。
回答by Serj Sagan
I had problems using typeof(Thing)
, eventually this worked for me:
我在使用时遇到问题typeof(Thing)
,最终这对我有用:
var thisThing = (new Thing()).GetType();
foreach (var property in thisThing.GetProperties())
{
// ...
}