Sdílet prostřednictvím


Použití odchylek v delegátech (C# and Visual Basic)

Když přiřadíte delegáta, metoda Kovariance a contravariance poskytují flexibilitu pro odpovídající typ delegáta s podpis metody.Kovariance umožňuje metoda mít návratový typ, který je odvozený více než je definováno v delegáta.Contravariance umožňuje metoda, která má typy parametrů, které jsou odvozené méně než typ delegáta.

Příklad 1: kovariance

Description

Tento příklad ukazuje použití Delegáti s metodami, které mají návratové typy, které jsou odvozeny z návratový typ podpisu delegáta.Typ dat vrácených DogsHandler je typu Dogs, který je odvozen z Mammals typu, který je definován v delegáta.

Kód

Class Mammals
End Class 

Class Dogs
    Inherits Mammals
End Class 
Class Test
    Public Delegate Function HandlerMethod() As Mammals
    Public Shared Function MammalsHandler() As Mammals
        Return Nothing 
    End Function 
    Public Shared Function DogsHandler() As Dogs
        Return Nothing 
    End Function 
    Sub Test()
        Dim handlerMammals As HandlerMethod = AddressOf MammalsHandler
        ' Covariance enables this assignment. 
        Dim handlerDogs As HandlerMethod = AddressOf DogsHandler
    End Sub 
End Class
class Mammals{}
class Dogs : Mammals{}

class Program
{
    // Define the delegate. 
    public delegate Mammals HandlerMethod();

    public static Mammals MammalsHandler()
    {
        return null;
    }

    public static Dogs DogsHandler()
    {
        return null;
    }

    static void Test()
    {
        HandlerMethod handlerMammals = MammalsHandler;

        // Covariance enables this assignment.
        HandlerMethod handlerDogs = DogsHandler;
    }
}

Příklad 2: Contravariance

Description

Tento příklad ukazuje použití Delegáti s metodami, které mají parametry typu, které jsou základní typy typ parametru podpisu delegáta.Contravariance můžete pomocí jedné obslužné rutiny události namísto samostatné rutiny.Můžete například vytvořit obslužnou rutinu události, která přijímá EventArgs vstupní parametr a použít jej s Button.MouseClick událost, která odešle MouseEventArgs , zadejte jako parametr a také s TextBox.KeyDown událost, která odešle KeyEventArgs parametr.

Kód

' Event hander that accepts a parameter of the EventArgs type. 
Private Sub MultiHandler(ByVal sender As Object,
                         ByVal e As System.EventArgs)
    Label1.Text = DateTime.Now
End Sub 

Private Sub Form1_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

    ' You can use a method that has an EventArgs parameter, 
    ' although the event expects the KeyEventArgs parameter. 
    AddHandler Button1.KeyDown, AddressOf MultiHandler

    ' You can use the same method  
    ' for the event that expects the MouseEventArgs parameter. 
    AddHandler Button1.MouseClick, AddressOf MultiHandler
End Sub
// Event hander that accepts a parameter of the EventArgs type. 
private void MultiHandler(object sender, System.EventArgs e)
{
    label1.Text = System.DateTime.Now.ToString();
}

public Form1()
{
    InitializeComponent();

    // You can use a method that has an EventArgs parameter, 
    // although the event expects the KeyEventArgs parameter. 
    this.button1.KeyDown += this.MultiHandler;

    // You can use the same method  
    // for an event that expects the MouseEventArgs parameter. 
    this.button1.MouseClick += this.MultiHandler;

}

Viz také

Referenční dokumentace

Použití odchylek pro obecné delegáty Func a Action (C# and Visual Basic)

Koncepty

Odchylky v delegátech (C# and Visual Basic)