I have a custom attribute BacksCachethat I use to mark fields in a class that should be "defaulted" when certain conditions are met.
I am 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 the class inheritance hierarchy is flat. That is, if type- this is a type that declares all attribute fields, everything is fine. As soon as I have a class Afrom which descends class B( B : A) and Bhas some attribute fields, all things fall apart: only the fields declared Aare detected and are "defaulted".
, , private private volatile, .
, ?