Reflecting a private field from a base class

前端 未结 5 1267
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 07:34

Here is the structure:

MyClass : SuperClass2

SuperClass2 : SuperClass1

superClass2 is in Product.Web and SuperClass1 is in the .NET System.Web assemb

5条回答
  •  情深已故
    2020-11-29 07:53

    I think you need to add the System.Reflection.BindingFlags.Instance flag. Use | to combine it with the NonPublic flag.

    EDIT:

    Looks like BrokenGlass has it right. I wrote the following quick test.

    var fields = test.GetType().BaseType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
    foreach (var field in fields)
    {
         System.Console.WriteLine(field.Name);
    }
    

    It correctly reports the field you were looking for. (Test is derived from BaseValidator)

提交回复
热议问题