Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type.GetProperties Method

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?

like image 743
Joan Venge Avatar asked Aug 07 '09 18:08

Joan Venge


1 Answers

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);
like image 129
JaredPar Avatar answered Sep 25 '22 19:09

JaredPar