反射和委托平时大家不怎么会用到,看完一段时间过后又遗忘的无影无踪了。今天看到博客园首页有这篇文章,顺便点进去看一下,感觉还不错。贴过来供以后查阅方便,亦能为理解这两概念的学者提供参考。
引用地址:http://www.cnblogs.com/last/archive/2009/10/31/1593498.html
闲来无事,写了一段 反射与委托的 Demo。
我尽量加了注释,
希望能给还不太了解反射与委托的朋友以启示。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45


1 namespace ThreadDemo
2 {
3 /// <summary>
4 /// 委托
5 /// </summary>
6 class Delegate
7 {
8 public delegate int testDelegate(int i,int j);//定义委托类型
9 static void Main(string[] args)
10 {
11 Delegate d = new Delegate();
12 d.DataInfo();
13 }
14 public void getDeltegate(testDelegate dg,int i,int j)//委托类型的传参
15 {
16 Console.WriteLine(dg(i,j).ToString());//调用被委托的方法
17 Console.ReadLine();
18 }
19 /// <summary>
20 /// 动态调用委托
21 /// </summary>
22 public void DataInfo()
23 {
24 //调用委托
25 getDeltegate(new testDelegate(DelegateInfor),2,3);
26 getDeltegate(new testDelegate(DelegateInfor2),1,1);
27 getDeltegate(new testDelegate(DelegateInfor3),-1,-1);
28 }
29
30 public int DelegateInfor(int i,int j)
31 {
32 return i+j;
33 }
34 public int DelegateInfor2(int i, int j)
35 {
36 return i + j;
37 }
38 public int DelegateInfor3(int i, int j)
39 {
40 return i + j;
41 }
42 }
以上两段实例,代码量较少,但是麻雀虽小,五脏俱全。
全当作参考资料吧。