Csharp/CSharp Tutorial/Reflection/Field

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Deeper Reflection: iterate through the fields of the class

using System;
using System.Reflection;
class MyClass
{
    MyClass() {}
    static void Process()
    {
    }
    
    public int MyFunction(int i, Decimal d, string[] args)
    {
        return(0);
    }
    public int        value = 0;
    public float        log = 1.0f;
    public static int    value2 = 44;
}
class MainClass
{    
    public static void Main(String[] args)
    {
        // 
        Console.WriteLine("Fields of MyClass");
        Type t = typeof (MyClass);
        foreach (MemberInfo m in t.GetFields())
        {
            Console.WriteLine("{0}", m);
        }
        
    }
}
Fields of MyClass
Int32 value
Single log
Int32 value2

Get all fields from a Type

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

Get/set a field using a FieldInfo

using System;
using System.Reflection;
using System.Windows.Forms; 
  public class Class1
  {
      static void Main( string[] args )
      {
        Type type = typeof(MyClass);
        object o = Activator.CreateInstance(type);
      
             FieldInfo field = type.GetField("text", BindingFlags.NonPublic | BindingFlags.Instance);
             field.SetValue(o, "www.domain.ru");
             string text = (string)field.GetValue(o);
        
             Console.WriteLine(text);
        
      }
      private static void OnChanged(object sender , System.EventArgs e)
      {
        Console.WriteLine(((MyClass)sender).Text);
      }
  }
  public class MyClass
  {
    private string text;
    
    public string Text
    {
      get{ return text; }
      set
      { 
        text = value;
        OnChanged(); 
      }
    }
    
    private void OnChanged()
    {
      if( Changed != null )
        Changed(this, System.EventArgs.Empty); 
    }
    
    public event EventHandler Changed;
  }

List Fields

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

Reflecting On Members Of A Type

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
   public class Tester
   {
      public static void Main()
      {
         Type theType = Type.GetType("System.Reflection.Assembly" );
         Console.WriteLine("\nSingle Type is {0}\n", theType );
         MemberInfo[] mbrInfoArray =theType.GetMembers();
         foreach ( MemberInfo mbrInfo in mbrInfoArray )
         {
            Console.WriteLine( "{0} is a {1}",mbrInfo, mbrInfo.MemberType );
         }
      }
   }