Reputation: 331052
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?
Upvotes: 6
Views: 4968
Reputation: 754745
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);
Upvotes: 18