博客
关于我
委托-利用GetInvocationList处理链式委托
阅读量:569 次
发布时间:2019-03-11

本文共 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~~~

 

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

你可能感兴趣的文章
重载和重写的区别:
查看>>
搭建Vue项目步骤
查看>>
linux 编译出现的错误
查看>>
账号转账演示事务
查看>>
idea创建工程时错误提醒的是architectCatalog=internal
查看>>
SpringBoot找不到@EnableRety注解
查看>>
简易计算器案例
查看>>
在Vue中使用样式——使用内联样式
查看>>
Find Familiar Service Features in Lightning Experience
查看>>
Explore Optimization
查看>>
Kali Linux 内网渗透教程 - ARP欺骗攻击 | 超详细
查看>>
2020Java程序设计基础(华东交通大学)章节测试免费满分答案
查看>>
OpenCV-未定义符号:hb_font_funcs_set_variation_glyph_func
查看>>
小程序之wx:request(转)
查看>>
连接Oracle数据库经常报错?关于listener.ora和tnsnames.ora文件的配置
查看>>
解决数据库报ORA-02289:序列不存在错误
查看>>
map[]和map.at()取值之间的区别
查看>>
成功解决升级virtualenv报错问题
查看>>
【SQLI-Lab】靶场搭建
查看>>
Xception 设计进化
查看>>