views:

293

answers:

1

Hello,

I have this method and want to get all properties from the FieldInfos? How to get it?

  private static void FindFields(ICollection<FieldInfo> fields, Type t)
  {
     var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;

     foreach (var field in t.GetFields(flags))
     {
        fields.Add(field);
     }

     var baseType = t.BaseType;
     if (baseType != null)
     {
        FindFields(fields, baseType);
     }
  }

     var fields = new Collection<FieldInfo>();
     FindFields(fields, this.GetType());

Thanks.

Best regards.

+2  A: 

To get the value of a field for a specific object use GetValue and pass the object for which you want to get the value.

var fields = new Collection<FieldInfo>();
FindFields(fields, this.GetType()); 

foreach (var field in fields)
{
    Console.WriteLine( "{0} = {1}", field.Name , field.GetValue(this));
}
Courtney de Lautour