Delegate.GetInvocationList Méthode

Définition

Retourne la liste d'appel du délégué.Returns the invocation list of the delegate.

public:
 virtual cli::array <Delegate ^> ^ GetInvocationList();
public virtual Delegate[] GetInvocationList ();
abstract member GetInvocationList : unit -> Delegate[]
override this.GetInvocationList : unit -> Delegate[]
Public Overridable Function GetInvocationList () As Delegate()

Retours

Tableau de délégués représentant la liste d'appel du délégué actuel.An array of delegates representing the invocation list of the current delegate.

Exemples

L’exemple suivant assigne trois méthodes à un délégué.The following example assigns three methods to a delegate. Il appelle ensuite la GetInvocationList méthode pour obtenir le nombre total de méthodes assignées au délégué, pour exécuter les délégués dans l’ordre inverse, et pour exécuter les méthodes dont le nom n’inclut pas la sous-chaîne « file ».It then calls the GetInvocationList method to get a total count of the methods assigned to the delegate, to execute the delegates in reverse order, and to execute the methods whose name do not include the substring "File".

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

public class Example
{
   public static void Main()
   {
      Action<String> outputMessage = null;
      outputMessage += Console.WriteLine;
      outputMessage += OutputToFile;
      outputMessage += ShowMessageBox;

//       Dim output1 As Action(Of String) = AddressOf Console.WriteLine
//       Dim output2 As Action(Of String) = AddressOf OutputToFile 
//       Dim output3 As Action(Of String) = AddressOf MessageBox.Show
//       
//       outputMessage = [Delegate].Combine( { output1, output2, output3 } )
      Console.WriteLine("Invocation list has {0} methods.", 
                        outputMessage.GetInvocationList().Length);

      // Invoke delegates normally.
      outputMessage("Hello there!");
      Console.WriteLine("Press <Enter> to continue...");
      Console.ReadLine();
      
      // Invoke each delegate in the invocation list in reverse order.
      for (int ctr = outputMessage.GetInvocationList().Length - 1; ctr >= 0; ctr--) {
         var outputMsg = outputMessage.GetInvocationList()[ctr];
         outputMsg.DynamicInvoke("Greetings and salutations!");
      }
      Console.WriteLine("Press <Enter> to continue...");
      Console.ReadLine();

      // Invoke each delegate that doesn't write to a file.
      for (int ctr = 0; ctr < outputMessage.GetInvocationList().Length; ctr++) {  
         var outputMsg = outputMessage.GetInvocationList()[ctr];
         if (! outputMsg.GetMethodInfo().Name.Contains("File"))
            outputMsg.DynamicInvoke( new String[] { "Hi!" } );
         
      }
   }

   private static void OutputToFile(String s)
   {
      var sw = new StreamWriter(@".\output.txt");
      sw.WriteLine(s);
      sw.Close();
   }

   private static void ShowMessageBox(String s)
   {
      MessageBox.Show(s);
   }
}
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms

Module Example
   Public outputMessage As Action(Of String)
   
   Public Sub Main()
      Dim output1 As Action(Of String) = AddressOf Console.WriteLine
      Dim output2 As Action(Of String) = AddressOf OutputToFile 
      Dim output3 As Action(Of String) = AddressOf MessageBox.Show
      
      outputMessage = [Delegate].Combine( { output1, output2, output3 } )
      Console.WriteLine("Invocation list has {0} methods.", 
                        outputMessage.GetInvocationList().Count)

      ' Invoke delegates normally.
      outputMessage("Hello there!")
      Console.WriteLine("Press <Enter> to continue...")
      Console.ReadLine()
      
      ' Invoke each delegate in the invocation list in reverse order.
      For ctr As Integer = outputMessage.GetInvocationList().Count - 1 To 0 Step -1
          Dim outputMsg = outputMessage.GetInvocationList(ctr)
          outputMsg.DynamicInvoke("Greetings and salutations!")
      Next
      Console.WriteLine("Press <Enter> to continue...")
      Console.ReadLine()
      
      ' Invoke each delegate that doesn't write to a file.
      For ctr As Integer = 0 To outputMessage.GetInvocationList().Count - 1 
         Dim outputMsg = outputMessage.GetInvocationList(ctr)
         If Not outputMsg.GetMethodInfo().Name.Contains("File") Then
            outputMsg.DynamicInvoke( { "Hi!" } )
         End If
      Next
   End Sub
   
   Private Sub OutputToFile(s As String)
      Dim sw As New StreamWriter(".\output.txt")
      sw.WriteLine(s)
      sw.Close()
   End Sub
End Module

Remarques

Chaque délégué du tableau représente une seule méthode.Each delegate in the array represents exactly one method.

L’ordre des délégués dans le tableau correspond à l’ordre dans lequel le délégué actuel appelle les méthodes que ces délégués représentent.The order of the delegates in the array is the same order in which the current delegate invokes the methods that those delegates represent.

Sécurité

ReflectionPermission
en cas d’appel à liaison tardive via des mécanismes InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[])tels que.when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). Énumération associée:MemberAccessAssociated enumeration: MemberAccess

S’applique à