Search code examples
c#.netaccess-modifiers

GetProperty Method returns null C# .Net


Can someone please explain, why does the GetProperty method in System.Type returns null for properties that are declared as 'internal' but works for 'public'.

internal class Test{      
  public string ALocal { get; set; }
  internal string SLocal { get; set; }}

var test = new Test();
var testType = test.GetType();

var aProp = testType.GetProperty("ALocal"); => returns string Type
var sProp = testType.GetProperty("SLocal"); => returns null

I understand differences between internal or public modifiers.


Solution

  • GetProperty Method returns only public properties by default. You should include following flags

    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static 
    

    to get internal type

    MSDN: https://msdn.microsoft.com/en-us/library/zy0d4103(v=vs.110).aspx