类库探源——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 ...
随机推荐
- js 打印网页指定内容
function doPrint() { setTimeout(function() { bdhtml=window.document.body.innerHTML; sprnstr="&l ...
- bzoj 4016 [FJOI2014]最短路径树问题(最短路径树+树分治)
4016: [FJOI2014]最短路径树问题 Time Limit: 5 Sec Memory Limit: 512 MBSubmit: 426 Solved: 147[Submit][Stat ...
- JavaScript高级程序设计28.pdf
classList属性 在操作类名时需要通过className属性添加.删除和替换类名 <div class="bd user disabled">...</di ...
- python-面向对象(一)——开篇基础
面向对象编程(Object Oriented Programming,OOP,面向对象程序设计) 一.创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现, ...
- Lottery - CodeForces 589I(水)
题目大意:有N个球K个人,现在要给这N个球涂上K种颜色,要求使抽到球的不同颜色的概率一致(N确保是K的倍数),求出来至少要给多少个球重新涂上颜色. 分析:先求出来所有球的每种颜色的个数,然后不到平均数 ...
- JMeter入门(3):录制JMeter脚本
一般自己手动的设置JMeter会比较麻烦,如果一边操作页面,提交表单,一边能够自动生成JMeter的脚本,则非常方便: BadBoy:录制JMeter脚本: Donwload URL:http://w ...
- linux之access函数解析
[lingyun@localhost access_1]$ ls access.c 实例一: [lingyun@localhost access_1]$ cat access.c /******** ...
- jquery mobile图片自适应屏幕
jquery mobile中如果不给img标签指定宽度的话,无法达到自适应屏幕的效果,特此备注:width:100%;
- [MySQL] MySQL的自己主动化安装部署
有过MySQL运维的人应该都清楚,线上的MySQL一般都採用源代码编译,由于这样才干够依据企业的各自须要选择要编译的功能,尽管MySQL的源代码编译挺简单的,可是试想一下,假设你有几百台server同 ...
- MySQLdb安装和使用2
http://blog.chinaunix.net/uid-8487640-id-3183185.html MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: ...