return all instance fields of a type - CSharp System

CSharp examples for System:Type

Description

return all instance fields of a type

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from ww w  . ja v a2 s .  c  o  m

public class Main{
        /// <summary>
      /// return all instance fields of a type
      /// </summary>
      /// <param name="t"></param>
      /// <returns></returns>
      public static IEnumerable<FieldInfo> GetAllFields(Type t)
      {
         while (t != null)
         {
            // GetFields() will not return inherited private fields, so walk the inheritance hierachy
            foreach (var f in t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly))
            {
               if (f.GetCustomAttributes(typeof(DeepEqualsIgnoreAttribute), true).Length == 0)
                  yield return f;
            }
            t = t.BaseType;
         }
      }
}

Related Tutorials