Csharp/CSharp Tutorial/Reflection/Delegate — различия между версиями

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

Текущая версия на 12:17, 26 мая 2010

Create Delegate using reflection

using System;
using System.Reflection;
using System.Collections.Generic;
delegate void ComputeDelegate( Employee emp, Decimal percent );
public class Employee
{
    public Decimal Salary;
    public Employee( Decimal salary ) {
        this.Salary = salary;
    }
    public void ApplyRaiseOf( Decimal percent ) {
        Salary *= (1 + percent);
    }
}
public class MainClass
{
    static void Main() {
        List<Employee> employees = new List<Employee>();
        employees.Add( new Employee(20) );
        employees.Add( new Employee(50) );
        MethodInfo mi = typeof(Employee).GetMethod( "ApplyRaiseOf", BindingFlags.Public | BindingFlags.Instance );
        ComputeDelegate applyRaise = (ComputeDelegate ) Delegate.CreateDelegate( typeof(ComputeDelegate), mi );
        foreach( Employee e in employees ) {
            applyRaise( e, (Decimal) 0.10 );
            Console.WriteLine( e.Salary );
        }
    }
}
22.0
55.0

Get event from Type

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);
        
            EventInfo eventInfo = type.GetEvent("Changed");
        eventInfo.AddEventHandler(o, new EventHandler(Class1.OnChanged));
            ((MyClass)o).Text = "New Value";
      }
      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;
  }

Get/set private field using InvokeMember

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);
        
        type.InvokeMember("text", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance, null, o, new object[]{"C#"});
        string text = (string)type.InvokeMember("text", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, o, new object[]{});
       
      }
      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;
  }