简体   繁体   中英

Type.GetProperties exclude properties with operator []

When I call

Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)

I also get properties with [] operators. So for instance I have:

MyType
-> Property1
-> Property2[string] 

And the returned list of PropertyInfo contains both Property1 or Property2.

How do I exclude properties with operators?

I would prefer it to happen through bindingflags, but iterating through the PropertyInfo afterwords would be ok, but I can't see anything on the PropertyInfo class that indicates whether it has an operator.

我认为没有任何BindingFlags值可以从一开始就排除它们,但您可以使用PropertyInfo.GetIndexParameters()来过滤属性:如果属性未编入索引,则它没有索引参数。

您可以使用LINQ来解决此问题:

Type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(prop => prop.GetIndexParameters().Length == 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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