类库探源——System.Delegate
一、MSDN 描述
Delegate 类:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法。(是不是感觉很像C语言中的函数指针 :) )
命名空间: System
程序集: mscorlib.dll
说到 Delegate 就必须谈 MulticastDelagate
MulticastDelagate类 :表示多路广播委托;即,其调用列表中可以拥有多个元素的委托。
命名空间: System
程序集: mscorlib.dll
继承关系:

备注:
1. Delegate 类是委托类型的基类
2. MulticastDelegate 继承自Delegate(这是废话,上面的继承关系那么明显)
3. MulticastDelegate 拥有一个带有链接的委托列表,该列表称为调用列表(虚函数表 Virtual Method Table)
4. 如果MulticastDelegate 的委托列表中某一个委托执行时发生异常,则在这个委托后面的委托不会执行
5. C# 中 delegate 与 Delegate 和 MulticastDelagate 的关系:
在C#中用 delegate 关键字标示一个方法 编译器将自动生成一个继承自 System.MulticastDelegate 的以方法名为类名的类,且添加 BeginInvoke 和 EndInvoke 方法
using System; public delegate void CustomDelaget();
// 用 delegate 关键字 编译器将自动生成一个继承自 System.MulticastDelegate 的类
/*
.class public auto ansi sealed CustomDelaget
extends [mscorlib]System.MulticastDelegate
{
} // end of class CustomDelaget
*/
class App
{
static void Main()
{
CustomDelaget d = new CustomDelaget(Invoke);
d.Invoke();
} static void Invoke()
{
}
}

二、用 Action 和 Func 代替自定义委托
Action 表示没有返回值的委托
Func 表示有返回值的委托
话说Action 和 Func 程序集分得有点怪异,Func 和 Action 程序集一样,以 Action 为例
Action(T) --> Action(T1,T2,T3,T4,T5,T6,T8) 的程序集为 mscorlib.dll
Action(T1,T2,T3,T4,T5,T6,T8,T9) --> Action(T1,T2,T3,...,T16) 的程序集为 System.Core.dll
这给人最初设计折类库的人觉得最多8个参数就够了,后来发现8个参数又不够,又加了8个放在 System.Core.dll 下的感觉
三、异步
1. 基于委托的异步(传统异步)
.NET Framework 运行你用传统异步的方式调用任何方法,只要你定义与你所需要调用的方法具有相同签名的委托,CLR会自动为该委托加上 BeginInvoke 和 EndInvoke
BeginInvoke 方法启动异步调用。
定义:
IAsyncResult BeginInvoke(待异步执行方法的参数表,AsyncCallback 委托,object State)
EndInvoke 方法检索异步调用的结果
如委托有返回值
var 委托的返回值 = EndInvoke(IAsyncResult ar);
如果 委托没有返回值则为
EndInvoke(IAsyncResult ar);
例子:
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging; // 无返回值的委托
public delegate void StringHanlder(string msg); // 待返回值的委托
public delegate string StringHanlderV2(string msg);
class App
{
static void Main()
{
StringHanlder handler = new StringHanlder(PrintMsg);
handler.BeginInvoke("待打印信息",new AsyncCallback(CallBack),string.Format("从{0}线程跳转而来",Thread.CurrentThread.ManagedThreadId)); // 待返回值
StringHanlderV2 handler2 = new StringHanlderV2(PrintMsgV2);
handler2.BeginInvoke("待打印信息V2",new AsyncCallback(CallBackV2),string.Format("V2从{0}线程跳转而来",Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(*);
} static void PrintMsg(string msg)
{
Console.WriteLine(msg);
} static void CallBack(IAsyncResult ar)
{
AsyncResult result = (AsyncResult) ar;
StringHanlder caller = (StringHanlder) result.AsyncDelegate;
string formatString = (string) ar.AsyncState;
caller.EndInvoke(ar); Console.WriteLine(result);
Console.WriteLine(caller);
Console.WriteLine(formatString);
} static string PrintMsgV2(string msg)
{
Console.WriteLine(msg);
return "已成功打印";
}
static void CallBackV2(IAsyncResult ar)
{
AsyncResult result = (AsyncResult) ar;
StringHanlderV2 caller = (StringHanlderV2) result.AsyncDelegate;
string formatString = (string) ar.AsyncState;
var strRet = caller.EndInvoke(ar); Console.WriteLine(result);
Console.WriteLine(caller);
Console.WriteLine(formatString);
Console.WriteLine(strRet);
}
}
结果:

2. 基于事件的异步
2014-12-29 补充
委托的几种初始化方式(原始方式、方法、匿名方法、Lambda 方式)
using System; public delegate void MyDelegate();
class App
{
static void Main()
{
//MyDelegate d = new MyDelegate(Method1); // 原始方式
//MyDelegate d = Method1; // 方法形式
//MyDelegate d = delegate{Console.WriteLine("Method1");}; // 匿名方法
MyDelegate d = () =>Console.WriteLine("Method1"); // Lambda 表达式
d.Invoke();
} static void Method1()
{
Console.WriteLine("Method1");
}
}
未完
类库探源——System.Delegate的更多相关文章
- 类库探源——System.Configuration 配置信息处理
按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting ...
- 类库探源——System.Drawing.Bitmap
一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...
- 类库探源——System.Drawing
一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.D ...
- 类库探源——System.Math 和 Random
一.System.Math Math类:为三角函数.对数函数和其他通用数学函数提供常数和静态方法 命名空间: System 程序集 : mscorlib.dll 继承关系: 常用属性: Math. ...
- 类库探源——System.ValueType
一.MSDN描述 ValueType 类:提供值类型的基类 命名空间: System 程序集: mscorlib.dll 继承关系: 值类型包括:字符.整数.浮点.布尔.枚举.结构(其实字符.整数 ...
- 类库探源——System.Environment
Environment 类: 提供有关当前环境和平台的信息以及操作它们的方法.此类不能被继承. 命名空间: System 程序集: mscorlib.dll 继承关系: 常用属性(字段)和方法: ...
- 类库探源——System.Exception
一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集: mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...
- 类库探源——System.String
一.MSDN描述 String 类: 表示文本,即一系列的 Unicode 字符 命名空间 : System 程序集 : mscorlib.dll 继承关系: 备注: 1. 字符串是 Unicode ...
- 类库探源——System.Type
一.MSDN 描述 Type 类:表示类型声明:类类型.接口类型.数组类型.值类型.枚举类型.类型参数.泛型类型定义.以及开放或封闭构造的泛型类型. 命名空间: System 程序集:mscorlib ...
随机推荐
- [ZETCODE]wxWidgets教程四:菜单栏和工具栏
本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...
- 使用DNSAgent拦截特定域名
开发程序时,为方便测试,需要把本来发往abc.com的数据发到本地. 最简单的方法是直接在程序中修改,把abc.com修改为需要的地址. 但这样提交代码时,容易把调试地址给提交到服务器. 或是嵌入式设 ...
- [LeetCode] 74. Search a 2D Matrix 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 交叉编译中的 --sysroot 等等在编译时的作用
--sysroot=dir 的作用 如果在编译时指定了-sysroot=dir 就是为编译时指定了逻辑目录.编译过程中需要引用的库,头文件,如果要到/usr/include目录下去找的情况下,则会在前 ...
- Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全
http://blog.cnbluebox.com/blog/2014/07/01/cocoashen-ru-xue-xi-nsoperationqueuehe-nsoperationyuan-li- ...
- 反射——类(Class)
本文原创,转载请注明原处! 红色代表的是基本组件:包(Package),修饰符(modifier),类(Class),字段(Field),构造器(Constructor)和方法(Method). 黄色 ...
- 微信开发第6章 通过accesstoken获取用户粉丝列表
上一章我们讲解到open_id获取用户基本信息,那么open_id哪儿来的呢?就是粉丝列表中可以看到的.本次讲解如何获取粉丝列表. 获取粉丝列表 可以查看文档 http://mp.weixin.qq. ...
- Stackdump: 一个可以离线看stackoverflow的工具
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Stackdump: 一个可以离线看stackoverflow的工具.
- [转]3proxy 二级代理配置样例
转自:http://www.cnblogs.com/airsong23/p/3893094.html 适应情况: 有时,我们的机器HOST-A只能通过代理服务器HOST-B才可以访问internet, ...
- unity3d中namespace的使用注意问题
最近在重构游戏中的老一套逻辑,同时要加入新的功能逻辑,因为各种命名问题,就使用namespace进行控制.但是namespace在unity3d引擎中的使用与一般不同,需要注意一点:要作为Compon ...