好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

深入解析C#编程中泛型委托的使用

在看泛型委托之前还需要先了解委托的概念。
这里讲的委托有两种类型一种是有返回值的,另一种是事件委托。

?

//定义有返回值的委托

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);

     }

   }

}

上面我们定义及调用了泛型委托,接下来就来梆定委托。

?

private 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方法。

委托 可以定义自己的类型参数。引用泛型委托的代码可以指定类型参数以创建已关闭的构造类型,就像实例化泛型类或调用泛型方法一样,如下例所示:

?

public 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);

}

引用委托的代码必须指定包含类的类型变量,如下所示:

?

private static void DoWork( float [] items) { }

 

public static void TestStack()

{

   Stack< float > s = new Stack< float >();

   Stack< float >.StackDelegate d = DoWork;

}

根据典型设计模式定义事件时,泛型委托尤其有用,因为发送方参数可以为强类型,不再需要强制转换成 Object,或反向强制转换。

?

delegate 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;

}

dy("nrwz");

查看更多关于深入解析C#编程中泛型委托的使用的详细内容...

  阅读:84次