简体   繁体   English

使用反射从 class object 获取属性,其中属性应该是公共的并且具有获取或获取并设置两者

[英]Get properties from class object using reflection where property should be public and having get or get and set both

How to use BindingFlags with reflection to get the properties those are public and decorated with get value or get and set value both.如何使用带有反射的 BindingFlags 来获取那些是公共的并用 get value 或 get 和 set value 装饰的属性。

I used following code but it does not return any property.我使用了以下代码,但它不返回任何属性。

var properties = obj.GetType().GetProperties(BindingFlags.Public & BindingFlags.Instance & BindingFlags.GetProperty);

The other answer is correct, but lacking explanation.另一个答案是正确的,但缺乏解释。

Context语境

There is a counterintuitive operation here, whereby merging flags using the |这里有一个违反直觉的操作,即使用|合并标志。 operator creates a merged flag that semantically acts as an "and" value.运算符创建一个在语义上充当“与”值的合并标志。

This is related to how humans use "and" differently than cold logic does.这与人类使用“和”的方式与冷逻辑不同有关。 For example:例如:

Tom and Bob are able to fix the car.汤姆和鲍勃能够修理汽车。

It doesn't mean that Tom && Bob (both of them) must be present for the car to be fixed, it means that Tom || Bob这并不意味着Tom && Bob (他们俩)必须在场才能修好汽车,而是意味着Tom || Bob Tom || Bob (ie at least one of them) needs to be present for the car to be fixed. Tom || Bob (即至少其中一个)需要在场才能修理汽车。

Similarly, while "and" in English often means the logical OR, "or" in English often means the logical XOR.类似地,虽然英语中的“and”通常表示逻辑 OR,但英语中的“or”通常表示逻辑 XOR。

You can have a burger or a milkshake.你可以吃汉堡或奶昔。

This is generally taken to mean that you're allowed to choose one, not two, making it an exclusive or (XOR).这通常意味着您可以选择一个,而不是两个,使其成为异或(XOR)。

Always be very careful of "and/or" in English versus logic.始终要非常小心英语与逻辑中的“和/或”。 Humans are not as logical as you'd hope.人类并不像你希望的那样合乎逻辑。


Example例子

For the sake of example, let's say that为了举例,让我们说

BindingFlags.Public   = 0001
BindingFlags.Instance = 0010

What we want to achieve, is a combined flag where both flags are present, so 0011 .我们想要实现的是一个组合标志,其中两个标志都存在,所以0011

When you use & , the result will only contain 1-bits when both values have a 1-bit there.当您使用&时,当两个值都有 1 位时,结果将仅包含 1 位。

  0001    // BindingFlags.Public
  0010    // BindingFlags.Instance
& ----
  0000    // No flags

Since the result is "no flags" (all 0), you are asking GetProperties() to return you objects that don't have any binding flags set.由于结果是“无标志”(全为 0),因此您要求GetProperties()返回未设置任何绑定标志的对象。 And since BindingFlags is a comprehensive set of flags, there are no objects with no flags.而且由于BindingFlags是一组全面的标志,因此没有没有标志的对象。

When you use |当您使用| , the result will combine all 1-bits that at least one of the members has. ,结果将组合至少一个成员具有的所有 1 位。

  0001    // BindingFlags.Public
  0010    // BindingFlags.Instance
| ----
  0011    // BindingFlags.Public "and" BindingFlags.Instance

In this case, you are asking GetProperties() to return you objects that have both the Public and Instance flags set (ie their third and fourth bit must be 1)在这种情况下,您要求GetProperties()返回同时设置了PublicInstance标志的对象(即它们的第三和第四位必须为 1)

So, in short, in order to get a combined "A and B" flag, you must calculate A | B所以,简而言之,为了得到一个组合的“A和B”标志,你必须计算A | B A | B . A | B

You should not be using the bitwise AND ( & ), but the bitwise OR ( | ):您不应该使用按位与( & ),而是按位或( | ):

var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM