Get all fields from a Type : Field « Reflection « C# / CSharp Tutorial






using System;
using System.Reflection;

class MyClass
{
   public int Field1;
   public int Field2;
   public void Method1() { }
   public int  Method2() { return 1; }
}

class MainClass
{
   static void Main()
   {
      Type t = typeof(MyClass);
      FieldInfo[] fi = t.GetFields();

      foreach (FieldInfo f in fi)
         Console.WriteLine("Field : {0}", f.Name);

   }
}
Field : Field1
Field : Field2








19.4.Field
19.4.1.Get all fields from a Type
19.4.2.List Fields
19.4.3.Deeper Reflection: iterate through the fields of the class
19.4.4.Get/set a field using a FieldInfo
19.4.5.Reflecting On Members Of A Type
19.4.6.Get Type full name and base type and its members