VB.NET - Problème avec Delegate et GetMethod

Résolu
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 2 févr. 2011 à 14:37
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 2 févr. 2011 à 17:58
Bonjour !

Je rencontre une difficulté, comment donner le String d'une variable a GetMethod ?

ça fonctionne:
Dim mi As MethodInfo = GetType(DelegateFonction).GetMethod("SubTest", bf_Instance)

renvoie la méthode, nickel !

ça fonctionne pas:
Dim NomFonction As String = "SubTest"
Dim mi As MethodInfo = GetType(DelegateFonction).GetMethod(NomFonction, bf_Instance)

renvoie nothing ?

5 réponses

Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
2 févr. 2011 à 15:32
MAOuahahah, MISSION COMPLETED ^^
Une classe qui Delegue n'importe laquelle de vos SUB (fonction à venir).

Créez une class, exemple: c_Delegate
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


Testez la class et son test appelé: SubTest
        Dim ez As New c_Delegate.DelegateSub
        ez.Main("SubTest", 500, "Hello World !")
3
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
2 févr. 2011 à 14:51
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
2 févr. 2011 à 16:57
Pas mal ta signature, un peu grosse. Mais tu as oublié une version, VB7.1 (alias VB.Net 2003), sorti aussi en 2002, qui a quand même eu une existence bien plus important que VB7.0 (VB.Net2002, qui aura vécu approximativement 6 mois)




[i][b]---- Sevyc64 (alias Casy) ----
[hr]# LE PARTAGE EST NOTRE FORCE #/b/i
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
2 févr. 2011 à 17:55
Voila, correction faite et j'ai fait plus jolie ^^
Merci casy.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
2 févr. 2011 à 17:58
PS: Pour info j'ai regardé la bannière d'Henry comme modèle.
0
Rejoignez-nous