在日常使用委托时,有以下常用方法

方法名称 说明
 Clone   创建委托的浅表副本。
 GetInvocationList   按照调用顺序返回此多路广播委托的调用列表。
 GetMethodImpl   返回由当前的 MulticastDelegate 表示的静态方法。
 GetObjectData   用序列化该实例所需的所有数据填充 SerializationInfo 对象。
 MemberwiseClone   创建当前 Object 的浅表副本。
 RemoveImpl   调用列表中移除与指定委托相等的元素

GetInvocationList() 的用途

当委托有多个返回值时

当你编写一个 delegate委托 或 Func<>泛型委托 ,并为实例绑定多个方法时,每个方法都有一个返回值。可能会遇到这种情况:

  class Program
{
public static string a(string str)
{
Console.WriteLine("方法a");
return str+"方法a";
}
public static string b(string str)
{
Console.WriteLine("方法b");
return str + "方法b";
}
public static string c(string str)
{
Console.WriteLine("方法c");
return str + "方法c";
}
static void Main(string[] args)
{
Func<string, string> func=a;
func += b;
func += c;
Console.WriteLine(func("测试"));
Console.ReadKey();
} }

调用委托后,只能获取到最后一个调用方法的返回值。


使用 GetInvocationList()

GetInvocationList() 能够返回 这个委托的方法链表。

通过使用循环,把每个方法顺序调用一次,每次循环中都会产生当前调用方法的返回值。

    class Program
{
public static string a(string str)
{
Console.WriteLine("方法a");
return str+"方法a";
}
public static string b(string str)
{
Console.WriteLine("方法b");
return str + "方法b";
}
public static string c(string str)
{
Console.WriteLine("方法c");
return str + "方法c";
}
static void Main(string[] args)
{
Func<string, string> func=a;
func += b;
func += c;
var funclist = func.GetInvocationList();
foreach (Func<string, string> f in funclist)
{
Console.WriteLine(f("测试"));
}
Console.ReadKey();
}

  

相当于把委托里顺序调用的方法分离成一个列表,通过循环调用,循环获取。

最新文章

  1. 一些关于HTTP协议、表单和……的备忘
  2. [HDOJ4609]3-idiots(FFT,计数)
  3. [vijos P1023] Victoria的舞会3
  4. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map
  5. mobile端
  6. [DP之普通系列]
  7. 用js实现2048小游戏
  8. J2EE学习从菜鸟变大鸟之八 企业级项目开发的思考
  9. XBMC源代码分析 6:视频播放器(dvdplayer)-文件头(以ffmpeg为例)
  10. SNMP相关的RFC建议和链接
  11. Apache为mysql以及自己的项目设置虚拟路径
  12. 【angularjs】pc端使用angular搭建项目,实现导出excel功能
  13. tomcat双击startup.bat启动时闪退
  14. 怎样提供一个好的移动API接口服务/从零到一[开发篇]
  15. mysql 常用,使用经验
  16. 高性能JavaScript(数据存取)
  17. 福州大学软工1816 K 班助教总结
  18. ASP.NET中Literal,只增加纯粹的内容,不附加产生html代码
  19. Android Studio 解决Fetching android sdk component information加载过久问题
  20. 使用word 2010 发布csdn博客

热门文章

  1. Jenkins 中创建项目时没有Maven项目怎么办
  2. dapper 多对多查询对象和对象列表
  3. WPF判断两个时间大小避免误差
  4. 【原】zookeeper集群配置常见问题说明
  5. SparkGraphx计算指定节点的N度关系节点
  6. 转:zip 和 unzip 的参数说明
  7. C#学习笔记-代理模式
  8. Django,COOKIES,SESSION完成用户登入
  9. &quot;Error: ANDROID_HOME is not set and &quot;android&quot; command not in your PATH. You must fulfill at least one of these conditions.&quot;.
  10. 关于电脑宽带显示连接 qq可以登录 但是无法上网的问题