GetProperty Method returns null C# .Net

十年热恋 提交于 2020-01-07 05:04:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33271391/getproperty-method-returns-null-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!