C# 委托类似于C函数指针示例使用:

 



1 using System;
2 using System.Reflection;
3
4 namespace ConsoleApp7
5 {
6 class Program
7 {
8 delegate void PrintHello();
9
10 static void print1()
11 {
12 // 获取当前方法名
13 Console.WriteLine(MethodBase.GetCurrentMethod().Name + " run....");
14 }
15 static void print2()
16 {
17 Console.WriteLine(MethodBase.GetCurrentMethod().Name + " run....");
18 }
19 static void print3()
20 {
21 Console.WriteLine(MethodBase.GetCurrentMethod().Name + " run....");
22 }
23
24 static void Main(string[] args)
25 {
26 PrintHello test_print_del;
27 test_print_del = print1;
28 test_print_del += print2;
29 test_print_del += print3;
30
31 test_print_del();
32 Console.WriteLine("=======移除方法=======");
33 test_print_del -= print2;
34 test_print_del();
35
36 Console.ReadKey();
37 }
38 }
39 }


 

运行结果终端打印:

C# 委托简单示例_方法名