다음을 통해 공유


Delegate.GetInvocationList 메서드

정의

대리자의 호출 목록을 반환합니다.

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()

반환

Delegate[]

현재 대리자의 호출 목록을 나타내는 대리자의 배열입니다.

예제

다음 예제에서는 대리자에게 세 가지 메서드를 할당합니다. 그런 다음 메서드를 호출 GetInvocationList 하여 대리자에게 할당된 메서드의 총 수를 가져와서 대리자를 역순으로 실행하고 이름에 부분 문자열 "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);
   }
}
open System
open System.IO
open System.Reflection
open System.Windows.Forms

let outputToFile (s: string) =
    use sw = new StreamWriter(@".\output.txt")
    sw.WriteLine s

let showMessageBox s =
    MessageBox.Show s |> ignore

let outputMessage =
    Delegate.Combine(
        Action<string>(Console.WriteLine),
        Action<string>(outputToFile),
        Action<string>(showMessageBox))
    :?> Action<string>

printfn $"Invocation list has {outputMessage.GetInvocationList().Length} methods."

// Invoke delegates normally.
outputMessage.Invoke "Hello there!"
printfn "Press <Enter> to continue..."
stdin.ReadLine() |> ignore

// Invoke each delegate in the invocation list in reverse order.
for i = outputMessage.GetInvocationList().Length - 1 downto 0 do
    let outputMsg = outputMessage.GetInvocationList()[i]
    outputMsg.DynamicInvoke "Greetings and salutations!"
    |> ignore

printfn "Press <Enter> to continue..."
stdin.ReadLine() |> ignore

// Invoke each delegate that doesn't write to a file.
for i = 0 to outputMessage.GetInvocationList().Length - 1 do
    let outputMsg = outputMessage.GetInvocationList()[i]
    if not (outputMsg.GetMethodInfo().Name.Contains "File") then
        outputMsg.DynamicInvoke "Hi!"
        |> ignore
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

설명

배열의 각 대리자는 정확히 하나의 메서드를 나타냅니다.

배열의 대리자 순서는 현재 대리자가 해당 대리자가 나타내는 메서드를 호출하는 순서와 같습니다.

적용 대상