How can I get non-public properties of a type via reflection?
Asked
Active
Viewed 4,176 times
1
-
What do you need that for? My first impulse is: Rethink your design. – Martin Hennings Sep 22 '10 at 08:13
-
Yes. Check out http://stackoverflow.com/questions/95910/find-a-private-field-with-reflection - i think it has all that you need – Jagmag Sep 22 '10 at 08:14
-
I've tried Type.GetProperties(). But it returns only public properties, but I need internal as well. – StuffHappens Sep 22 '10 at 08:16
2 Answers
4
Yes, you can. Specify BindingFlags.NonPublic
in your call to GetProperties()
.
class Program
{
static void Main(string[] args)
{
var f = new Foo();
foreach (var fi in f.GetType().GetProperties(
BindingFlags.NonPublic | BindingFlags.Instance))
{
Console.WriteLine(fi);
}
}
}
public class Foo
{
private string Prop { get; set; }
}

Michael Petrotta
- 59,888
- 27
- 145
- 179
1

Vinay B R
- 8,089
- 2
- 30
- 45
-
2This will return an empty array, you also need to specify BindingFlags.Instance or BindingFlags.Static. – Thomas Levesque Sep 22 '10 at 08:30