1

How can I get non-public properties of a type via reflection?

Nic
  • 12,220
  • 20
  • 77
  • 105
StuffHappens
  • 6,457
  • 13
  • 70
  • 95

2 Answers2

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

Use

myType.GetProperties(BindingFlags.NonPublic);

try this link for details.

Vinay B R
  • 8,089
  • 2
  • 30
  • 45