C#高级开发之委托和事件(DelegateEvent) 四

一、什么是委托             

委托:委托其本质实际上是一个类(一个密封的类中类) 继承自System.MulticastDelegate类,类中包含了那些Invoke,BeginInvoke,EndInvoke等方法,IL反编译查看其本质如下如

委托的声明 其语法上就是一个去掉Delegate关键字后没有方法体的方法。委托专门为方法服务的把方法作为参数可传递, 方法和委托相互协调合作。

二、委托的用处和作用  

 1. 委托的声明以及实例化

委托可以将方法当作方法的参数传递(委托的实例化和类的实例化类似委托其本质就是类) ,委托的参数和返回类型,都要和具体的委托方法的一致(专门为方法服务),

语法:访问修饰符 delegate 返回值类型 委托名称(参数1, 参数1, 参数n);

如 public delegate bool thanDelegate(Student student);//委托的声明 返回类型为bool 参数为Student类

如下示例 展示了委托的声明,委托实例化及调用 

 1  public delegate void NoReturnNoPara();//委托的声明
 2         public delegate void NoReturnWithPara(int x, int y);
 3         public delegate int WithReturnNoPara();
 4         public void show()
 5         {
 6             Console.WriteLine("测试委托");
 7             Student student = new Student()
 8             {
 9                 Name = "张三",
10                 Mobile = "13282825652",
11                 Password = "123",
12                 Account = "123"
13             };
14             //把方法包装成变量,Invoke的时候自动执行方法
15             NoReturnNoPara method = new NoReturnNoPara(this.Dothing);//委托的实例化(类似类的实例化) 把Dothing方法变量化
16             method.Invoke();//委托的调用(执行刚刚赋值该变量的方法)
17             Dothing();
18         }
19         public void Dothing()
20         {
21             Console.WriteLine("this is Dothing");
22         }
23 }
View Code

 2.把方法当作方法的参数去传递(方法参数化委托)

委托解耦,减少重复代码,实现代码共用

 1   public void ShowStudents()
 2         {GetSutdent(students, 1);
 3             GetSutdent(students, SelectStudent);//把方法当作参数参数传递
 4             GetSutdent(students, SelectStudentAgeName);//把方法当作参数参数传递
 5 
 6         }
 7         public delegate bool thanDelegate(Student student);//委托的声明
 8         /// <summary>
 9         /// type:不同值 不同判断 传一个值 执行对应的行为逻辑
10         /// 传递方法(把行为逻辑当参数传递)
11         /// </summary>
12         /// <param name="source"></param>
13         /// <param name="type"></param>
14         /// <returns></returns>
15         public List<Student> GetSutdent(List<Student> source, thanDelegate than)// 传递委托 把方法传进去
16         {
17             List<Student> result = new List<Student>();
18             foreach (Student student in source)
19             {
20                 if (than.Invoke(student))//2委托的调用
21                 {
22                     result.Add(student);
23                 }
24             }
25             return result;
26         }
27         public bool SelectStudent(Student student)
28         {
29             return student.Age > 28;
30         }
31         public bool SelectStudentName(Student student)
32         {
33             return student.Name.Length > 2;
34         }
35         public bool SelectStudentAgeName(Student student)
36         {
37             return student.Age > 25 && student.Password.Length > 3;
38         }
39         /// <summary>
40         /// type:不同值 不同判断 传一个值 执行对应的行为逻辑
41         /// </summary>
42         /// <param name="source"></param>
43         /// <param name="type"></param>
44         /// <returns></returns>
45         public List<Student> GetSutdent(List<Student> source, int type)
46         {
47             List<Student> result = new List<Student>();
48             foreach (Student student in source)
49             {
50                 if (type == 1)
51                 {
52                     if (student.Age > 28)
53                     {
54                         result.Add(student);
55                     }
56                 }
57                 else if (type == 1)
58                 {
59                     if (student.Name.Length > 2)
60                     {
61                         result.Add(student);
62                     }
63                 }
64                 else if (student.Age > 25 && student.Password.Length > 3)
65                 {
66                     result.Add(student);
67                 }
68             }
69             return result;
70         }
View Code

 3.委托的 异步多线程

异步 委托,所有的异步多线程都是建立在异步委托之上的,离开了异步委托是无法产生异步多线程。

public delegate int WithReturnNoPara();//返回值为int类型的委托
        public void show()
        { //委托 beginInvoke,endInvoke
            {
                WithReturnNoPara withReturnNoPara = new WithReturnNoPara(GetInt);
                withReturnNoPara.Invoke();
                IAsyncResult  result= withReturnNoPara.BeginInvoke(null, null);//委托异步调用
                withReturnNoPara.EndInvoke(result);
            }
        }
        public int GetInt()
        {
            return 8;
        }
View Code

 4.多播委托

posted @ 2019-06-14 16:56  youzhangcai  阅读(407)  评论(0编辑  收藏  举报