[英]Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection
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? 谁知道我做错了什么?
If you specify any BindingFlags
, then you need to specify explicitly what properties you want to get. 如果指定任何
BindingFlags
,则需要明确指定要获取的属性。 For example: 例如:
sometype.GetProperties (BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance);
To summarize: 总结一下:
if you use the GetProperties()
overload (without parameters), you will get all public properties . 如果您使用
GetProperties()
重载(不带参数),您将获得所有公共属性 。
on the other hand, if you use the GetProperties(BindingFlags)
overload (the one which accepts a BindingFlags
parameter), then you need to specify a bitwise OR of at least one from each group of the following flags: 另一方面,如果使用
GetProperties(BindingFlags)
重载(接受BindingFlags
参数的重载),则需要在以下标志的每组中指定至少一个的按位OR:
BindingFlags.Instance
/ BindingFlags.Static
(instance vs static properties), BindingFlags.Instance
/ BindingFlags.Static
(实例与静态属性), BindingFlags.Public
/ BindingFlags.NonPublic
(public vs non-public properties). 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. 例如,要获取公共静态属性,您需要调用
GetProperties(BindingFlags.Public | BindingFlags.Static)
才能获得结果。 Calling only GetProperties(BindingFlags.Public)
or GetProperties(BindingFlags.Static)
won't return any results. 仅调用
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 filter flags can be used to define which nested types to include in the search:
以下BindingFlags 过滤器标志可用于定义要包含在搜索中的嵌套类型:
- You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.您必须指定
BindingFlags.Instance
或BindingFlags.Static
才能获得返回。- Specify
BindingFlags.Public
to include public properties in the search.指定
BindingFlags.Public
以在搜索中包含公共属性。- Specify
BindingFlags.NonPublic
to include non-public methods (that is, private, internal, and protected methods) in the search.指定
BindingFlags.NonPublic
以在搜索中包含非公共方法(即私有,内部和受保护方法)。 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;指定
BindingFlags.FlattenHierarchy
以在层次结构中包含公共和受保护的静态成员; private static members in inherited classes are not included.不包括继承类中的私有静态成员。
The following BindingFlags modifier flags can be used to change how the search works:
以下BindingFlags 修饰符标志可用于更改搜索的工作方式:
BindingFlags.DeclaredOnly
to search only the properties declared on the Type, not properties that were simply inherited.BindingFlags.DeclaredOnly
仅搜索在Type上声明的属性,而不是仅继承的属性。
You need to expand your BindingsFlag a bit. 你需要稍微扩展你的BindingsFlag。 They need to at least include what accessibility level and instance vs. static in order to get anything meaningful back.
他们需要至少包括可访问性级别和实例与静态,以便获得任何有意义的东西。
I think what you are actually looking for is the following 我认为您实际需要的是以下内容
var flags = BindingFlags.DeclaredOnly
| BindingFlags.Instance
| BindingFlags.Public;
someType.GetProperties(flags);
Default (member) Specifies no binding flag.
默认(成员)指定无绑定标志。
You must specify Instance or Static along with Public or NonPublic or no members will be returned.
您必须与Public或NonPublic一起指定Instance或Static,否则将不返回任何成员。
Hence unless you specify the binding flags you get nothing. 因此,除非您指定绑定标志,否则什么也得不到。
I had problems using typeof(Thing)
, eventually this worked for me: 我在使用
typeof(Thing)
遇到了问题,最终这对我有用:
var thisThing = (new Thing()).GetType();
foreach (var property in thisThing.GetProperties())
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.