通过类的reflection属性获取,而不是从inheritance类获取

class Parent { public string A { get; set; } } class Child : Parent { public string B { get; set; } } 

我只需要得到财产乙,没有财产甲但是

 Child.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) 

返回两个属性:/

你应该添加BindingFlags.DeclaredOnly到你的标志,即:

 typeof(Child).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly) 

尝试使用DeclaredOnly绑定标志。 它应该将返回的属性限制为仅在您感兴趣的类中声明的属性。下面是一个代码示例:

 PropertyInfo[] properties = typeof(Child).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly); 

添加BindingFlags.DeclaredOnly

Type.cs :在这种情况下使用DeclaredOnlyLookup

  private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; internal const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;