Get All Fields from Type - CSharp System.Reflection

CSharp examples for System.Reflection:FieldInfo

Description

Get All Fields from Type

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w ww. j av a 2s . c  o m*/

public class Main{
  public static FieldInfo[] GetAllFields(this Type type)
   {
      // http://stackoverflow.com/a/1155549/154165
      
      if (type == null)
      {
         return new FieldInfo[0];
      }
      
      BindingFlags flags =
         BindingFlags.Public |
            BindingFlags.NonPublic |
            BindingFlags.Instance |
            BindingFlags.DeclaredOnly;
      
      return type.GetFields(flags).Concat(GetAllFields(type.BaseType)).ToArray();
   }
}

Related Tutorials