类库探源——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 ...
随机推荐
- The Same Game": A Simple Game from Start to Finish3
视图: 画出你的游戏界面 前面,我们的文档对象中已经初始化了游戏板对象,接下来我们需要显示这些信息给用户了. 第一步是添加代码,来重新设置我们的窗口尺寸.缺省的窗口尺寸不是我们想要的,我们将重写OnI ...
- Java Web SSH框架总是无法写入无法读取Cookie
不关乎技术,关乎一个小Tips: 默认情况下,IE和Chrome内核的浏览器会认为http://localhost为无效的域名,所以不会保存它的cookie,使用http://127.0.0.1访问程 ...
- strcpy,memcpy,内存块重叠
前段时间准备面试,看了一些库函数的实现,在看到memcpy时,发现有处理source和destination所指内存有重叠的情况,而strcpy没有,特别模仿库函数写了这个函数,并进行了测试.以下是具 ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- Bzoj 3505: [Cqoi2014]数三角形 数论
3505: [Cqoi2014]数三角形 Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description
- Vi和Vim的区别及联系
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.vim的这些优势主要体现在以下几个方面:1.多级撤消我们知道在vi里,按 u只能撤消上次命 ...
- python Day 2 - 编写数据库模块
在一个Web App中,所有数据,包括用户信息.发布的日志.评论等,都存储在数据库中.在awesome-python-app中,我们选择MySQL作为数据库. Web App里面有很多地方都要访问数据 ...
- windows mobile 6.5 隐藏 左下角(左上角)的开始按钮 叉号关闭按钮
其实做起来很简单,但是国内的网站就是找不到. 1.开始按钮原来的界面是这样的: windows mobile 6.0界面: windows mobile 6.5.X界面: 修改一个windows mo ...
- linq 学习笔记(一)
First: 找到符合条件的第一记录,就返回了,不管后面还有多少数据. Single: 先将记录都梳理一次,再找到符合要求的唯一记录. Single():操作一个集合,同时强要求只有一个对象匹配, ...
- action使用大全
1.Intent的用法: (1)Action跳转 1. 使用Action跳转,当程序AndroidManifest.xml中某一个 Activity的IntentFilter定义了包含Action, ...