
委托-利用GetInvocationList处理链式委托
发布日期:2021-05-14 04:38:02
浏览次数:9
分类:博客文章
本文共 3952 字,大约阅读时间需要 13 分钟。
在利用委托进行函数代理的时候,我们习惯于用+=来把一个符合条件的委托加入委托链之中,如果加入了多个这样的函数,怎么一一对这些函数取返回值呢?请看下面的一个实例:
View Code
using System;namespace GetInvocationListDaemonCAPP{ public delegate string TestDelegate(); public class Program { static void Main(string[] args) { DelegateClass delegateClass = new DelegateClass(); TestMethodOne one = new TestMethodOne(); TestMethodTwo two = new TestMethodTwo(); TestMethodThree three = new TestMethodThree(); TestMethodFour four = new TestMethodFour(); delegateClass.testDelegate += one.Say; delegateClass.testDelegate += two.Say; delegateClass.testDelegate += three.Say; delegateClass.testDelegate += four.Say; delegateClass.InvokeDelegate(); Console.ReadKey(); } } public class DelegateClass { public TestDelegate testDelegate; public void InvokeDelegate() { if (null != testDelegate) { string resultStr = testDelegate(); Console.WriteLine(resultStr); } } } public class TestMethodOne { public string Say() { return "You called me from TestMethodOne~~~"; } } public class TestMethodTwo { public string Say() { return "You called me from TestMethodTwo~~~"; } } public class TestMethodThree { public string Say() { return "You called me from TestMethodThree~~~"; } } public class TestMethodFour { public string Say() { return "You called me from TestMethodFour~~~"; } }}
在这个示例中,我用了一个委托代理了四个类型相同,返回值相同的函数,那么当我要获取这些函数的返回值的时候,会得到什么样的结果呢?
You called me from TestMethodFour~~~
结果就是上面的输出,原来,像这种方式的委托操作,会保留最后一个输出,前面几个都被OverWrite掉了。
为了解决这个问题,GetInvocationList方法出现了。 View Code
using System;namespace GetInvocationListDaemonCAPP{ public delegate string TestDelegate(); public class Program { static void Main(string[] args) { DelegateClass delegateClass = new DelegateClass(); TestMethodOne one = new TestMethodOne(); TestMethodTwo two = new TestMethodTwo(); TestMethodThree three = new TestMethodThree(); TestMethodFour four = new TestMethodFour(); delegateClass.testDelegate += one.Say; delegateClass.testDelegate += two.Say; delegateClass.testDelegate += three.Say; delegateClass.testDelegate += four.Say; delegateClass.InvokeDelegate(); Console.ReadKey(); } } public class DelegateClass { public TestDelegate testDelegate; public void InvokeDelegate() { if (null != testDelegate) { //遍历委托链表 foreach (Delegate dele in testDelegate.GetInvocationList()) { //类型转换 TestDelegate delegateClass = (TestDelegate)dele; //调用并 得到返回结果 string resultStr = delegateClass(); Console.WriteLine(resultStr); } } } } public class TestMethodOne { public string Say() { return "You called me from TestMethodOne~~~"; } } public class TestMethodTwo { public string Say() { return "You called me from TestMethodTwo~~~"; } } public class TestMethodThree { public string Say() { return "You called me from TestMethodThree~~~"; } } public class TestMethodFour { public string Say() { return "You called me from TestMethodFour~~~"; } }}
这样操作后,得到的运行结果如下:
You called me from TestMethodOne~~~You called me from TestMethodTwo~~~You called me from TestMethodThree~~~You called me from TestMethodFour~~~
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年05月02日 10时23分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
04_Mysql配置文件(重要参数)
2019-03-06
python 序列化及其相关模块(json,pickle,shelve,xml)详解
2019-03-06
js编写动态时钟
2019-03-06
JavaSE总结
2019-03-06
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
2019-03-06
Python IO编程
2019-03-06
CSS入门总结
2019-03-06
使用 TortoiseGit 时,报 Access denied 错误
2019-03-06
基于 HTML5 WebGL 的污水处理厂泵站自控系统
2019-03-06
[系列] Go gRPC 调试工具
2019-03-06
django-表单之模型表单渲染(六)
2019-03-06
c++之程序流程控制
2019-03-06
一位年轻而优秀的.NET开发者的成长点滴
2019-03-06
如何使用ABP进行软件开发(1) 基础概览
2019-03-06
AlwaysOn配置时在连接步骤时报错(35250)
2019-03-06
排序算法之总结
2019-03-06
微软云Linux服务器 Mysql、tomcat远程连接错误解决办法
2019-03-06
Python数据分析(二): Numpy技巧 (2/4)
2019-03-06