Condividi tramite


Uso della varianza per i delegati generici Func e Action (Visual Basic)

Questi esempi illustrano come usare la covarianza e la controvarianza nei Func delegati generici e Action per consentire il riutilizzo dei metodi e offrire maggiore flessibilità nel codice.

Per altre informazioni sulla covarianza e sulla controvarianza, vedere Varianza nei delegati (Visual Basic).For more information about covariance and contravariance, see Variance in Delegates (Visual Basic).

Uso dei delegati con parametri di tipo covariante

L'esempio seguente illustra i vantaggi del supporto alla covarianza nei delegati generici Func. Il FindByTitle metodo accetta un parametro del String tipo e restituisce un oggetto del Employee tipo. Tuttavia, è possibile assegnare questo metodo al Func(Of String, Person) delegato perché Employee eredita Person.

' 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

Uso di delegati con parametri di tipo controvariante

L'esempio seguente illustra i vantaggi del supporto della controvarianza nei tipi di delegati generici Action. Il AddToContacts metodo accetta un parametro del Person tipo. Tuttavia, è possibile assegnare questo metodo al Action(Of Employee) delegato perché Employee eredita Person.

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

Vedere anche