TDaver
TDaver

Reputation: 7264

Reflection not finding protected field of nested type

I have a class, which has a protected nested class, and a protected readonly field of the nested class' type. My framework calls

o.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic);

on an instance of the type, I can see the field from debugger, but the call doesn't return it. Why?

Upvotes: 7

Views: 4436

Answers (2)

Justin Niessner
Justin Niessner

Reputation: 245429

You also need to include BindingFlags.Instance

Instance - Specifies that instance members are to be included in the search.

from

BindingFlags Enumeration (System.Reflection)

Upvotes: 11

Reed Copsey
Reed Copsey

Reputation: 564433

You should also specify BindingFlags.Instance if it's a non-static field.

If it's a static field, add BindingFlags.Static and BindingFlags.FlattenHierarchy.

Upvotes: 3

Related Questions