Delegate.GetInvocationList 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回委托的调用列表。
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);
}
}
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
注解
数组中的每个委托只表示一个方法。
数组中委托的顺序与当前委托调用这些委托表示的方法的顺序相同。