博客
关于我
委托-利用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 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>