日期:2014-05-18 浏览次数:20675 次
将方法签名与代理类型匹配时,协变和逆变为您提供了一定程度的灵活性。协变允许方法具有的派生返回类型比委托中定义的更多。逆变允许方法具有的派生参数类型比委托类型中的更少。
一、逆变
对于下面的三个代理类型:
delegate void EventHandler (object sender, EventArgs e) delegate void KeyPressEventHandler (object sender, KeyPressEventArgs e) delegate void MouseEventHandler (object sender, MouseEventArgs e)
System.DateTime lastActivity; public Form1() { InitializeComponent(); lastActivity = new System.DateTime(); this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs } // Event hander for any event with an EventArgs or // derived class in the second parameter private void MultiHandler(object sender, System.EventArgs e) { lastActivity = System.DateTime.Now; }
协变是针对返回值类型具有可转换类型的情况,下面是协变的例子:
class Mammals { } class Dogs : Mammals { } class Program { // Define the delegate. public delegate Mammals HandlerMethod(); public static Mammals FirstHandler() { return null; } public static Dogs SecondHandler() { return null; } static void Main() { HandlerMethod handler1 = FirstHandler; // Covariance allows this delegate. HandlerMethod handler2 = SecondHandler; } }