I have the class B and its parent class A, both in namespace Domain.
- Class A, has the private field a;
- Class B, has the private field b;
Then I have a Reflection Util in namespace Reflect.
If I use this line
instanceOfB.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Public | BindingFlags.Instance );
to to find all fields (a & b), I get only b. But when I make a
protected or public I find them too.
What do I need to do to find the private fields of the base class too?
Best Answer
This is the documented behaviour:
If you need to get private fields, you'll need to ask the base type. (Use
Type.BaseType
to find the base type, and callGetFields
on that.)