简体   繁体   English

Type.GetProperties使用operator []排除属性

[英]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. 返回的PropertyInfo列表包含Property1或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迭代,但我在PropertyInfo类上看不到任何指示它是否有运算符的东西。

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

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

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

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

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