tags:

views:

154

answers:

3

I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection.

However I need to find out if the property is public or protected. eg:

public string Name{get;set;}
protected  int Age{get;set;}

The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this?

+6  A: 

Since properties are just syntactic sugar over a pair of get/set methods, there's no such thing as "accessibility" of a property reflection-wise. Rather, you'll have to find out accessibility levels of get and set methods separately. To that end, retrieve appropriate MethodInfo objects with GetGetMethod and GetSetMethod methods, and from there are various IsPrivate, IsPublic and other methods and properties.

Anton Gogolev
you beat me to it...
Thomas Levesque
A: 

Not that I know of. You can ask two times, though - once only for the public properties.

TomTom
+2  A: 

You need to look at the methodInfo of each get & set method eg :

PropertyInfo pi = ...;
bool isPublic = pi.GetGetMethod(true).IsPublic();
bool isProtected= pi.GetGetMethod(true).IsFamily();

It seems to be the IsFamily property that indicates if a method is protected..

Richard Friend