List Fields : Field « Reflection « C# / CSharp Tutorial






using System;
using System.Reflection;

public interface IFaceOne
{
  void MethodA();
}

public interface IFaceTwo
{
  void MethodB();
}

public class MyClass: IFaceOne, IFaceTwo
{
  public enum MyNestedEnum{}
  
  public int myIntField;
  public string myStringField;

  public void myMethod(int p1, string p2)
  {
  }

  public int MyProp
  {
    get { return myIntField; }
    set { myIntField = value; }
  }

  void IFaceOne.MethodA(){}
  void IFaceTwo.MethodB(){}
}

public class MainClass
{
  public static void Main(string[] args)
  {
    MyClass f = new MyClass();

       Type t = f.GetType();
    FieldInfo[] fi = t.GetFields();
    foreach(FieldInfo field in fi)
      Console.WriteLine("Field: {0}", field.Name);
  }
}
Field: myIntField
Field: myStringField








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