类库探源——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 ...
随机推荐
- jquery mobile 前言
引用js: <link rel="stylesheet" href="../jquery.mobile-1.1.0/jquery.mobile-1.1.0.css& ...
- 监控CPU和内存的使用
监控CPU和内存的使用: #!/bin/bash #script to capture system statistics outfile=/home/rainbow/test/file.csv da ...
- devi into python 笔记(二)元组 变量声明 和列表解析
元组tuple: 类似list,只是tuple是不可变的list.类似java的String都是不可改变的.注意:tuple没有方法(有待考证),不可以像list那样那个list.pop 或者list ...
- Cogs 309. [USACO 3.2] 香甜的黄油 dijkstra,堆,最短路,floyd
题目:http://cojs.tk/cogs/problem/problem.php?pid=309 309. [USACO 3.2] 香甜的黄油 ★★ 输入文件:butter.in 输出文件 ...
- DAL与数据库类型的对应关系
MSSQL Server类型 DbType类型 C#类型 MySql类型 DbType类型 C#类型 bit Boolean bool bit(1) Boolean bool tinyint ...
- Dijkstra算法求解最短路径分析
最短路径是图论算法中的经典问题.图分为有向图.无向图,路径权值有正值.负值,针对不同的情况需要分别选用不同的算法.在维基上面给出了各种不同的场景应用不同的算法的基本原则:最短路问题. 针对无向图,正权 ...
- Yii PHP 框架分析(二)
Yii PHP 框架分析(二)作者:wdy http://hi.baidu.com/delphiss/blog/item/54597af595085ad3f3d38552.html Yii是基于组件( ...
- AVL树的左旋右旋理解 (转)
AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...
- nyoj 119 士兵杀敌(三)【线段树区间最大值最小值差】
士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...
- MVC母版面,子页的脚本生成在最后