VB.Net/Generics/Generic Delegate

Материал из VB Эксперт
Перейти к: навигация, поиск

Generic delegate can point to any method taking a single argument (specified at the time of creation)

  
Option Explicit On
Option Strict On
Public Delegate Sub MyGenericDelegate(Of T)(ByVal arg As T)
Public Delegate Sub MyDelegate(ByVal arg As Object)
Module Program
  Sub Main()
    Dim d As New MyGenericDelegate(Of Integer)(AddressOf IntegerTarget)
    d(100)
    Dim d2 As New MyGenericDelegate(Of String)(AddressOf StringTarget)
    d2("Cool!")
  End Sub
  Public Sub IntegerTarget(ByVal arg As Integer)
    Console.WriteLine("You passed me a {0} with the value of {1}", arg.GetType().Name, arg)
  End Sub
  Public Sub StringTarget(ByVal arg As String)
    Console.WriteLine("You passed me a {0} with the value of {1}", arg.GetType().Name, arg)
  End Sub
End Module