Csharp/C Sharp/Generics/Generic delegate

Материал из .Net Framework эксперт
Версия от 11:45, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

a generic delegate being initialized with a normal method

 
using System;
public delegate void DelegateClass<T>(T data);
public class Starter {
    public static void Main() {
        DelegateClass<string> del = MethodA;
        del("data");
    }
    public static void MethodA(string data) {
        Console.WriteLine("MethodA ({0})", data);
    }
}


A simple generic delegate

using System;
// Declare a generic delegate.
delegate T GenericDelegate<T>(T v);
class GenDelegateDemo {
  static int sum(int v) {
    int result = 0;
    for(int i=v; i>0; i--)
      result += i;
    return result;
  }
  static string reflect(string str) {
    string result = "";
    foreach(char ch in str)
      result = ch + result;
    return result;
  }
  public static void Main() {
    GenericDelegate<int> intDel = sum;
    Console.WriteLine(intDel(3));
    GenericDelegate<string> strDel = reflect;
    Console.WriteLine(strDel("Hello"));
  }
}


Convert event to use generic delegate

/*
Example code from 

Chapter 18 - Generics
C# 2.0: The Complete Reference, Second Edition
by Herbert Schildt 
McGraw-Hill/Osborne ? 2006
*/       
       
using System;
class MyEventArgs : EventArgs {
  public int eventnum;
}
delegate void MyEventHandler<T, V>(T source, V args);
class MyEvent {
  static int count = 0;
  public event MyEventHandler<MyEvent, MyEventArgs> SomeEvent;
  public void OnSomeEvent() {
    MyEventArgs arg = new MyEventArgs();
    if(SomeEvent != null) {
      arg.eventnum = count++;
      SomeEvent(this, arg);
    }
  }
}
class X {
  public void handler<T, V>(T source, V arg) where V : MyEventArgs {
    Console.WriteLine("Event " + arg.eventnum + " received by an X object.");
    Console.WriteLine("Source is " + source);
    Console.WriteLine();
  }
}
class Y {
  public void handler<T,V>(T source, V arg) where V : MyEventArgs {
    Console.WriteLine("Event " + arg.eventnum + " received by a Y object.");
    Console.WriteLine("Source is " + source);
    Console.WriteLine();
  }
}
class Test {
  public static void Main() {
    X ob1 = new X();
    Y ob2 = new Y();
    MyEvent evt = new MyEvent();
    evt.SomeEvent += ob1.handler;
    evt.SomeEvent += ob2.handler;
    evt.OnSomeEvent();
    evt.OnSomeEvent();
  }
}


Generic Anonymous Methods

 
public delegate void ADelegate<T>(T tvalue);
public class MyClass{
    public void MethodA() {
        ADelegate<int> del=delegate(int var) {
        };
    }
}
public class YClass<T>{
    public delegate void BDelegate(T tValue);
    public void MethodA() {
        BDelegate del=delegate(T tValue) {
        };
    }
}


Generic Delegate and non-generic delegate

 
using System;
using System.Collections.Generic;
using System.Text;
public delegate void MyGenericDelegate<T>(T arg);
public delegate void MyDelegate(object arg);
class Program {
    static void Main(string[] args) {
        MyDelegate d = new MyDelegate(MyTarget);
        d("More string data");
        MyDelegate d2 = new MyDelegate(MyTarget);
        d2(9);
        MyGenericDelegate<string> strTarget =
            new MyGenericDelegate<string>(StringTarget);
        strTarget("Some string data");
        MyGenericDelegate<int> intTarget = IntTarget;
        intTarget(9);
        Console.ReadLine();
    }
    static void MyTarget(object arg) {
        if (arg is int) {
            int i = (int)arg;
            Console.WriteLine("++arg is: {0}", ++i);
        }
        if (arg is string) {
            string s = (string)arg;
            Console.WriteLine("arg in uppercase is: {0}", s.ToUpper());
        }
    }
    static void StringTarget(string arg) {
        Console.WriteLine("arg in uppercase is: {0}", arg.ToUpper());
    }
    static void IntTarget(int arg) {
        Console.WriteLine("++arg is: {0}", ++arg);
    }
}


Generics and Delegates

 
using System;
public delegate void DelegateClass(int data);
public class Starter {
    public static void Main() {
        DelegateClass del1 = MethodA<int>;
        del1(5);
        DelegateClass del2 = MethodA;
        del1(10); // inferred
    }
    public static void MethodA<T>(T data) {
        Console.WriteLine("MethodA ({0})", data);
    }
}