Freigeben über


Using Variance for Func and Action Generic Delegates (Visual Basic) (Verwenden von Varianz für die generischen Delegaten Func und Action (Visual Basic))

In diesen Beispielen wird die Verwendung von Kovarianz und Kontravarianz in den Func und Action generischen Delegaten veranschaulicht, um die Wiederverwendung von Methoden zu ermöglichen und mehr Flexibilität in Ihrem Code zu bieten.

Weitere Informationen zu Ko- und Kontravarianz finden Sie unter Varianz bei Delegaten (Visual Basic).

Verwenden von Delegaten mit kovarianten Typparametern

Das folgende Beispiel veranschaulicht die Vorteile der Unterstützung von Kovarianz in generischen Func-Delegaten. Die FindByTitle Methode verwendet einen Parameter des String Typs und gibt ein Objekt des Employee Typs zurück. Sie können diese Methode jedoch dem Func(Of String, Person) Delegaten zuweisen, da Employee von Person erbt.

' Simple hierarchy of classes.
Public Class Person
End Class

Public Class Employee
    Inherits Person
End Class

Class Finder
    Public Shared Function FindByTitle(
        ByVal title As String) As Employee
        ' This is a stub for a method that returns
        ' an employee that has the specified title.
        Return New Employee
    End Function

    Sub Test()
        ' Create an instance of the delegate without using variance.
        Dim findEmployee As Func(Of String, Employee) =
            AddressOf FindByTitle

        ' The delegate expects a method to return Person,
        ' but you can assign it a method that returns Employee.
        Dim findPerson As Func(Of String, Person) =
            AddressOf FindByTitle

        ' You can also assign a delegate
        ' that returns a more derived type to a delegate
        ' that returns a less derived type.
        findPerson = findEmployee
    End Sub
End Class

Verwendung von Delegaten mit kontravarianten Typparametern

Im folgenden Beispiel werden die Vorteile der Unterstützung von Kontravarianz in generischen Action-Delegaten veranschaulicht. Die AddToContacts Methode verwendet einen Parameter des Person Typs. Sie können diese Methode jedoch dem Action(Of Employee) Delegaten zuweisen, da Employee von Person erbt.

Public Class Person
End Class

Public Class Employee
    Inherits Person
End Class

Class AddressBook
    Shared Sub AddToContacts(ByVal person As Person)
        ' This method adds a Person object
        ' to a contact list.
    End Sub

    Sub Test()
        ' Create an instance of the delegate without using variance.
        Dim addPersonToContacts As Action(Of Person) =
            AddressOf AddToContacts

        ' The Action delegate expects
        ' a method that has an Employee parameter,
        ' but you can assign it a method that has a Person parameter
        ' because Employee derives from Person.
        Dim addEmployeeToContacts As Action(Of Employee) =
            AddressOf AddToContacts

        ' You can also assign a delegate
        ' that accepts a less derived parameter
        ' to a delegate that accepts a more derived parameter.
        addEmployeeToContacts = addPersonToContacts
    End Sub
End Class

Siehe auch