Jens Mikkelsen
Jens Mikkelsen

Reputation: 2712

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.

Upvotes: 4

Views: 1631

Answers (2)

Matt
Matt

Reputation: 2682

You can use LINQ to solve this:

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

Upvotes: 4

user743382
user743382

Reputation:

I don't think there's any BindingFlags value to exclude them right from the start, but you can use PropertyInfo.GetIndexParameters() to filter properties: if a property is not indexed, it has no index parameters.

Upvotes: 5

Related Questions