博客
关于我
委托-利用GetInvocationList处理链式委托
阅读量:582 次
发布时间: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/

你可能感兴趣的文章
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>
mysql 输入密码秒退
查看>>
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
MySQL 高性能优化规范建议
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-8.0.16 的安装与配置
查看>>