日期:2014-05-18  浏览次数:20642 次

C#高级程序设计(五)——代理中的协变和逆变

将方法签名与代理类型匹配时,协变和逆变为您提供了一定程度的灵活性。协变允许方法具有的派生返回类型比委托中定义的更多。逆变允许方法具有的派生参数类型比委托类型中的更少。

一、逆变

对于下面的三个代理类型:

delegate void EventHandler (object sender, EventArgs e)
delegate void KeyPressEventHandler (object sender, KeyPressEventArgs e)
delegate void MouseEventHandler (object sender, MouseEventArgs e)

由于KeyPressEventArgs和MouseEventArgs都继承自EventArgs,因此,如果你有一个EventArgs参数的方法,你总能用一个KeyPressEventArgs类型的参数去掉用,因此用类似EventHandler签名的方法去创建KeyPressEventHandler类型的代理对象就显得有意义了。这正是C#2所支持的特性。

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