博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个委托包含委托链(Combine),getinvocationlist的例子
阅读量:4043 次
发布时间:2019-05-24

本文共 4020 字,大约阅读时间需要 13 分钟。

微软docs上的例子,可以参考学习:

https://docs.microsoft.com/en-us/dotnet/api/system.delegate.getinvocationlist?view=netframework-4.5

C#using System;using System.IO;using System.Reflection;using System.Windows.Forms;public class Example{
public static void Main() {
Action
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
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
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); }}
VB:Imports System.IOImports System.ReflectionImports System.Windows.FormsModule 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
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
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 SubEnd Module另外一个例子,委托事件返回的类型是Task: Public Sub Main() Dim action As Func(Of String, Task) = AddressOf upload Dim action1 As Func(Of String, Task) = AddressOf upload1 Dim delegateComb = [Delegate].Combine({action, action1}) Await delegateComb.DynamicInvoke(autoCommand) End Sub Private Async Function upload1(ByVal msg As String) As Task Await Task.Delay(3000) End Function Private Async Function upload2(ByVal msg As String) As Task Await Task.Delay(3000) End Function

转载地址:http://ksmdi.baihongyu.com/

你可能感兴趣的文章
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>