【问题标题】:GetProperty Method returns null C# .NetGetProperty 方法返回 null C# .Net
【发布时间】:2016-01-21 03:55:02
【问题描述】:

有人可以解释一下,为什么 System.Type 中的 GetProperty 方法对声明为“内部”但适用于“公共”的属性返回 null。

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

我了解内部修饰符和公共修饰符之间的区别。

【问题讨论】:

标签: c# .net access-modifiers


【解决方案1】:

GetProperty 方法默认只返回公共属性。 您应该包括以下标志

BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static 

获取内部类型

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

【讨论】:

  • BindingFlags.Static 不需要,因为它是实例成员。
猜你喜欢
  • 2022-01-15
  • 2018-05-23
  • 1970-01-01
  • 2012-10-14
  • 2020-10-31
  • 2019-08-06
  • 1970-01-01
  • 2013-11-19
相关资源
最近更新 更多