I’m trying to reflect over some class properties and set them programmatically, but it looks like one of my PropertyInfo filters isn’t working:
//Get all public or private non-static properties declared in this class (no inherited properties) - that have a getter and setter.
PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty );
I’m getting an error on the line
pi.SetValue(this, valueFromData, null);
Because the property has only a get{} method, no set{} method.
My question is, why wasn’t this property filtered out of props? I thought that was the purpose of BindingFlags.SetProperty.
The property not getting filtered out is:
public String CollTypeDescription
{
get { return _CollTypeDescription; }
}
Note that I want to filter properties that won’t work ahead of time because I’m listing them all at once. I don’t want to use pi.GetSetMethod() after the fact to determine whether I can use the setter.
From the documentation:
BindingFlags.SetPropertyandBindingFlags.GetPropertydo not filter properties that are missing setters or getters, respectively.To check if a property can be set, use the
CanWriteproperty.