Imports System Imports System.Threading Imports System.Reflection Imports System.Runtime.InteropServices Imports System.Security.Permissions Public Delegate Sub DS(ByVal MesParametres() As Object) Public Class DelegateFonction Protected id As Integer Public Sub New(ByVal id As Integer) Me.id = id End Sub Public Sub SwitchDelegate(ByVal MesParametres() As Object) Dim NomFonction As String = MesParametres(0) Dim Parametres(MesParametres.Length - 2) As Object Array.ConstrainedCopy(MesParametres, 1, Parametres, 0, MesParametres.Length - 1) Select Case NomFonction Case "SubTest" Dim MonChiffre As Integer = CType(Parametres(0), Integer) Dim MonMessage As String = CType(Parametres(1), String) SubTest(MonChiffre, MonMessage) Exit Sub Case Else MessageBox.Show("Cette fonction n'existe pas !", "Delegate Impossible ID: " & id, MessageBoxButtons.OK, MessageBoxIcon.Stop) End Select End Sub Shared Sub SubTest(ByVal MonChiffre As Integer, ByVal MonMessage As String) Dim mBox As String = Nothing mBox = "Welcome dans la fonction SUBTEST !" & ControlChars.CrLf mBox &= "Prouve-le !..." & ControlChars.CrLf mBox &"[MonChiffre * 2] " & (MonChiffre * 2).ToString & ControlChars.CrLf mBox &"[MonMessage] " & MonMessage MessageBox.Show(mBox, _ "Delegation Réussit", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Class Public Class c_Delegate Public Class DelegateSub Public Sub Main(ByVal ParamArray Parametres() As Object) Dim NewFonction As New DelegateFonction(0) Dim bf_Instance As BindingFlags = (BindingFlags.Public Or BindingFlags.Instance) Dim bf_Static As BindingFlags = (BindingFlags.Public Or BindingFlags.Static) Dim mi As MethodInfo = GetType(DelegateFonction).GetMethod("SwitchDelegate", bf_Instance) Dim NewDelegate As DS Dim test As [Delegate] = [Delegate].CreateDelegate(GetType(DS), NewFonction, mi, False) If test IsNot Nothing Then NewDelegate = CType([Delegate].CreateDelegate(GetType(DS), Nothing, mi), DS) NewDelegate(Parametres) Else MessageBox.Show("Le tentative de delegation à échoué !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub End Class End Class
Dim ez As New c_Delegate.DelegateSub ez.Main("SubTest", 500, "Hello World !")
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question