I've got a custom attribute BacksCache
that I'm using to mark fields in a class that need to be "defaulted" when certain conditions are met.
I'm using the following code:
Type type = obj.GetType();
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);
foreach (FieldInfo field in fields)
{
foreach (Attribute at in field.GetCustomAttributes(true))
if (at is BacksCache)
{
field.SetValue(obj, Utils.DefaultValue(field.FieldType));
}
}
This code works fine, provided that the class inheritance hierarchy is flat. That is to say, if type
is the Type that declares all the attributed fields, everything's great. As soon as I have a class A
from which descends class B
(B : A
) and B
has some attributed fields things fall apart: only the fields declared by A
are detected and "defaulted".
The fields in question are private
or private volatile
, if that makes any difference.
Any ideas on how to fix this?