BindingFlagsのNonPublicについて
NonPublicって親クラスのメンバは対象にならないんですね。ドキュメントを読んでも、そうとはわからないですが。
ちなみに、staticにしても同じように親のprivateなプロパティは取得できないです。
[TestMethod]
public void TestBindigFlags()
{
var type = typeof(Derived);
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
Assert.IsNotNull(type.GetProperty("Aaa", flags));
Assert.IsNull(type.GetProperty("Bbb", flags)); //親クラスのprivateなプロパティは取得できない(nullがかえる)。
Assert.IsNotNull(type.GetProperty("Ccc", flags));
}public class Base
{
public string Aaa { get; set; }private string Bbb { get; set; }
}public class Derived : Base
{
private string Ccc { get; set; }
}
[TestMethod]
public void TestBindigFlagsForStaticProperties()
{
var type = typeof(Derived);
var flags = BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic;
Assert.IsNotNull(type.GetProperty("Aaa", flags));
Assert.IsNull(type.GetProperty("Bbb", flags)); //親クラスのprivateなプロパティは取得できない(nullがかえる)。
Assert.IsNotNull(type.GetProperty("Ccc", flags));
}public class Base
{
public static string Aaa { get; set; }private static string Bbb { get; set; }
}public class Derived : Base
{
private static string Ccc { get; set; }
}