class Parent {
public string A { get; set; }
}
class Child : Parent {
public string B { get; set; }
}
我只需要获得属性B,而无需获得属性A,但是
Child.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
返回两个属性:/
Answers:
您应该将BindingFlags.DeclaredOnly添加到标志中,即:
typeof(Child).GetProperties(System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.Instance
| System.Reflection.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;