在看泛型委托之前還需要先了解委托的概念。
這裡講的委托有兩種類型一種是有返回值的,另一種是事件委托。
//定義有返回值的委托
public delegate string GenricDelegate<T, S>(T title, S author);
//定義事件委托。
public delegate void GenricDelegateEnent<E,P>(E Name,P Address);
public class GenericDelegateClass<V,F>
{
//聲明委托
public GenricDelegate<V, F> GdeleValue;
//聲明事件委托
public event GenricDelegateEnent<V, F> GdEvent = null;
public string GetValues(V title, F author)
{
//調用委托
return GdeleValue(title, author);
}
public GenericDelegateClass()
{
}
public void InvokeEvent(V name, F address)
{
if (GdEvent != null)
{
//調用委托
GdEvent(name, address);
}
}
}
上面我們定義及調用了泛型委托,接下來就來梆定委托。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19private void btnDelegate_Click(object sender, EventArgs e)
{
GenericDelegateClass<string, string> gd = new GenericDelegateClass<string, string>();
//將DelegateReturn事件梆定給GdeleValue
gd.GdeleValue = new GenricDelegate<string, string>(DelegateReturn);
//將GenericEvent事件梆定給GdEvent
gd.GdEvent += new GenricDelegateEnent<string, string>(GenericEvent<string,string>);
}
public string DelegateReturn<T,S>(T title,S author)
{
return title.ToString() + author;
}
private void GenericEvent<V, F>(V name, F address)
{
//
}
在這裡我們看到我在梆定DelegateReturn的時候並沒有帶泛型參數。在這裡的泛型參數其實是沒什麼意義的。因為他的類型取決於調用委托的方法的類型。也就是在前面那段代碼中InvokeEvent方法的類型,這裡的DelegateReturn要用泛型方法是可以隨時跟InvokeEvent的參數類型保持一至。這樣梆定後我們再來調用gd.GetValues("my generic post","fastyou");這樣調用的其實就是DelegateReturn的方法,這就是委托的好處了,同樣調用gd.InvokeEvent("my generic post","fastyou");就是GenericEvent方法。
委托 可以定義自己的類型參數。引用泛型委托的代碼可以指定類型參數以創建已關閉的構造類型,就像實例化泛型類或調用泛型方法一樣,如下例所示:
? 1 2 3 4public delegate void Del<T>(T item);
public static void Notify(int i) { }
Del<int> m1 = new Del<int>(Notify);
C# 2.0 版具有稱為方法組轉換的新功能,此功能適用於具體委托類型和泛型委托類型,並使您可以使用如下簡化的語法寫入上一行:
Del<int> m2 = Notify;
在泛型類內部定義的委托使用泛型類類型參數的方式可以與類方法所使用的方式相同。
class Stack<T>
{
T[] items;
int index;
public delegate void StackDelegate(T[] items);
}
引用委托的代碼必須指定包含類的類型變量,如下所示:
? 1 2 3 4 5 6 7private static void DoWork(float[] items) { }
public static void TestStack()
{
Stack<float> s = new Stack<float>();
Stack<float>.StackDelegate d = DoWork;
}
根據典型設計模式定義事件時,泛型委托尤其有用,因為發送方參數可以為強類型,不再需要強制轉換成 Object,或反向強制轉換。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24delegate void StackEventHandler<T, U>(T sender, U eventArgs);
class Stack<T>
{
public class StackEventArgs : System.EventArgs { }
public event StackEventHandler<Stack<T>, StackEventArgs> stackEvent;
protected virtual void OnStackChanged(StackEventArgs a)
{
stackEvent(this, a);
}
}
class SampleClass
{
public void HandleStackChange<T>(Stack<T> stack, Stack<T>.StackEventArgs args) { }
}
public static void Test()
{
Stack<double> s = new Stack<double>();
SampleClass o = new SampleClass();
s.stackEvent += o.HandleStackChange;