注:本文使用的例子是园子里JimmyZhang写的一个示例,根据需要略有更改,还请老张谅解 : )
文中用例出处:http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html
委托,个人理解最大的好处在于2点,1,可以直接将方法作为参数进行传递,避免了大量的if-else的判断。2,可扩展性比较好,减少了扩展程序时改动的地方。
示例的C#源码如下:
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Delegate
6 {
7
8 public delegate void GreetingDelegate( string name);
9
10 class Program
11 {
12 private static void GreetPeople( string name, GreetingDelegate MakeGreeting)
13 {
14 MakeGreeting(name);
15 }
16
17 private static void EnglishGreeting( string name)
18 {
19 Console.WriteLine( " Morning, " + name);
20 }
21
22 private static void ChineseGreeting( string name)
23 {
24 Console.WriteLine( " 早上好, " + name);
25 }
26
27 static void Main( string [] args)
28 {
29 GreetingDelegate delegate1;
30
31 delegate1 = new GreetingDelegate(EnglishGreeting);
32 GreetPeople( " Leon weng " , delegate1);
33
34 delegate1 = new GreetingDelegate(ChineseGreeting);
35 GreetPeople( " 老翁 " , delegate1);
36
37 Console.ReadKey();
38 }
39 }
40 }
如果不使用委托,我们在调用GreetPeople方法时必须给出要使用中文还是英文来打招呼,也许会用枚举什么的,而GreetPeople这个方法内部也需要对传递进来的值进行判断从而决定使用哪个方法完成这个打招呼动作。使用委托之后,我们可以直接将需要使用的方法同打招呼的对象(name)一起传进GreetPeople方法来完成这个操作。
这是一个很简单的示例,可我实在想知道这个过程在IL中是怎么实现的。于是,有了下面的IL代码:
看到IL后,Delegate实质上被声明成了一个公开类, (extends System.MulticastDelegate完成了什么动作呢)
我使用的是控制台程序集,那么Program到IL是什么样子呢,首先,包含了四个方法。ChineseGreeting,EnglishGreeting,GreetPeople,Main
注意,此时的GreetPeople方法的接收两个参数,string 和 class Delegate.GreetingDelegate,也就是说将真个类做为参数传递进去了。
ChineseGreeting,EnglishGreeting的IL代码都比较简单:
{
// 代码大小 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr bytearray (E9 65 0A 4E 7D 59 2C 00 20 00 ) // .e.N}Y,. .
IL_0006: ldarg.0
IL_0007: call string [mscorlib]System.String::Concat( string ,
string )
IL_000c: call void [mscorlib]System.Console::WriteLine( string )
IL_0011: nop
IL_0012: ret
} // end of method Program::ChineseGreeting
.method private hidebysig static void EnglishGreeting( string name) cil managed
{
// 代码大小 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr " Morning, "
IL_0006: ldarg.0
IL_0007: call string [mscorlib]System.String::Concat( string ,
string )
IL_000c: call void [mscorlib]System.Console::WriteLine( string )
IL_0011: nop
IL_0012: ret
} // end of method Program::EnglishGreeting
我们将关注的是GreetPeople,Main两个方法的IL代码:
class Delegate.GreetingDelegate MakeGreeting) cil managed
{
// 代码大小 10 (0xa)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldarg.0
IL_0003: callvirt instance void Delegate.GreetingDelegate::Invoke( string )
IL_0008: nop
IL_0009: ret
} // end of method Program::GreetPeople
.method private hidebysig static void Main( string [] args) cil managed
{
.entrypoint
// 代码大小 58 (0x3a)
.maxstack 3
.locals init ([ 0 ] class Delegate.GreetingDelegate delegate1)
IL_0000: nop
IL_0001: ldnull
IL_0002: ldftn void Delegate.Program::EnglishGreeting( string )
IL_0008: newobj instance void Delegate.GreetingDelegate::.ctor( object ,
native int)
IL_000d: stloc.0
IL_000e: ldstr " Leon weng "
IL_0013: ldloc.0
IL_0014: call void Delegate.Program::GreetPeople( string ,
class Delegate.GreetingDelegate)
IL_0019: nop
IL_001a: ldnull
IL_001b: ldftn void Delegate.Program::ChineseGreeting( string )
IL_0021: newobj instance void Delegate.GreetingDelegate::.ctor( object ,
native int)
IL_0026: stloc.0
IL_0027: ldstr bytearray ( 01 80 C1 7F )
IL_002c: ldloc.0
IL_002d: call void Delegate.Program::GreetPeople( string ,
class Delegate.GreetingDelegate)
IL_0032: nop
IL_0033: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
IL_0038: pop
IL_0039: ret
} // end of method Program::Main
我们看到GreetPeople接收了名字和方法,其实就相当于一个连接器,将名字对应到了应该使用的方法并invoke.
未完,待续。
本文转自wengyuli 51CTO博客,原文链接:http://blog.51cto.com/wengyuli/588589,如需转载请自行联系原作者