I have a class like this:
class ItemList
{
Int64 Count { get; set; }
}
and when I write this:
ItemList list = new ItemList ( );
Type type = list.GetType ( );
PropertyInfo [ ] props = type.GetProperties ( );
I get an empty array for props.
Why? Is it because GetProperties doesn't include automatic properties?
The problem is that GetProperties will only return Public properties by default. In C#, members are not public by default (I believe they are internal). Try this instead
var props = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
The BindingFlags enumeration is fairly flexible. The above combination will return all non-public instance properties on the type. What you likely want though is all instance properties regardless of accessibility. In that case try the following
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var props = type.GetProperties(flags);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With