Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 738
TDaver Avatar asked Apr 01 '11 17:04

TDaver


2 Answers

You also need to include BindingFlags.Instance

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

from

BindingFlags Enumeration (System.Reflection)

like image 130
Justin Niessner Avatar answered Nov 10 '22 14:11

Justin Niessner


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.

like image 32
Reed Copsey Avatar answered Nov 10 '22 16:11

Reed Copsey