Udostępnij za pośrednictwem


Korzystanie z wariancji w delegatach (C# i Visual Basic)

Podczas przypisywania metody do oddelegowania, Kowariancja i kontrawariancja zapewnić elastyczność w zgodności z typem obiektu delegowanego przy użyciu podpisu metody.Kowariancja pozwala metoda ma typ zwracany jest bardziej pochodny, niż zdefiniowane w pełnomocnika.Kontrawariancja pozwala na metody, która ma typy parametrów, które są mniej pochodny, niż te, w polu Typ obiektu delegowanego.

Przykład 1: Kowariancja

Opis

Ten przykład demonstruje, jak można używać delegatów z metod, które mają zwracanych typów, które pochodzą z zwracany typ podpisu delegata.Dane zwrócone przez DogsHandler jest typu Dogs, co wynika z Mammals typu, który jest zdefiniowany w pełnomocnika.

Kod

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

Przykład 2: Kontrawariancja

Opis

Ten przykład demonstruje, jak można używać delegatów z metod, które mają parametry typu, które typy podstawowy typ parametru delegata podpisu.Z kontrawariancja można użyć jednego obsługi zdarzeń, zamiast oddzielnych obsługi.Na przykład, można utworzyć obsługi zdarzeń, który akceptuje EventArgs parametr wejściowy i używać go z Button.MouseClick zdarzenia, która wysyła MouseEventArgs typu jako parametr i z TextBox.KeyDown zdarzenia, która wysyła KeyEventArgs parametru.

Kod

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

}

Zobacz też

Informacje

Korzystanie z wariancji dla delegatów Func i Action (C# i Visual Basic)

Koncepcje

Wariancje w delegatach (C# i Visual Basic)