delegate关键字与Delegate类型

今天写了一个例子,能说明delegate和Delegate的不同,帖在下面,注释很全:
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Resources;
 5 using System.Reflection;
 6 using System.Globalization;
 7 
 8 namespace ConsoleApplication
 9 {
10   /*
11    * 在这个位置,由于delegate关键字,编译器会为我们生成一个类型 ConsoleApplication.TestDel (继承自System.MulticastDelegate)
12    * 而这个类型的构造器定义为:
13    * .method public hidebysig specialname rtspecialname 
14         instance void  .ctor(object 'object',
15                              native int 'method') runtime managed
16       {
17       } // end of method TestDel::.ctor
18    */
19   delegate int TestDel(int i);
20 
21   class UseDelegate
22   {
23     /*
24      * .method public hidebysig static string  Compute(class ConsoleApplication.TestDel del,
25                                                 int32 input) cil managed
26      */
27     public static string Compute(TestDel del, int input)
28     {
29       return del(input).ToString();
30     }
31 
32     /*
33      * .method public hidebysig static string  ComputeTwo(class [mscorlib]System.Delegate del,
34                                                    int32 input) cil managed
35      */
36     public static string ComputeTwo(Delegate del, int input)
37     {
38       return null;
39     }
40   }
41 
42 
43   class Program
44   {
45     static void Main(string[] args)
46     {
47       int k = 5;
48 
49       /*
50        *  IL_0004:  ldftn      int32 ConsoleApplication.Program::MyMethodOne(int32)
51           IL_000a:  newobj     instance void ConsoleApplication.TestDel::.ctor(object,
52                                                                                native int)
53           IL_000f:  ldloc.0
54           IL_0010:  call       string ConsoleApplication.UseDelegate::Compute(class ConsoleApplication.TestDel,
55                                                                               int32)
56           IL_0015:  call       void [mscorlib]System.Console::WriteLine(string)
57        */
58       Console.WriteLine(UseDelegate.Compute(MyMethodOne, k));
59       Console.WriteLine(UseDelegate.Compute(MyMethodTwo, k));
60 
61       /*
62        * IL_0034:  ldftn      int32 ConsoleApplication.Program::MyMethodOne(int32)
63         IL_003a:  newobj     instance void ConsoleApplication.TestDel::.ctor(object,
64                                                                              native int)
65         IL_003f:  ldloc.0
66         IL_0040:  call       string ConsoleApplication.UseDelegate::ComputeTwo(class [mscorlib]System.Delegate,
67                                                                                int32)
68         IL_0045:  call       void [mscorlib]System.Console::WriteLine(string)
69        */
70       Console.WriteLine(UseDelegate.ComputeTwo(new TestDel(MyMethodOne), k));
71 
72       Console.Read();
73     }
74 
75     private static int MyMethodOne(int i)
76     {
77       return (i++);
78     }
79 
80     private static int MyMethodTwo(int i)
81     {
82       return (i--);
83     }
84 
85   }
86 
87 }
posted @ 2009-08-31 14:16  EagleFish(邢瑜琨)  阅读(2159)  评论(0编辑  收藏  举报