Delegate

1.基本类:

  1. public class Student
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5.  
  6. public void Study()
  7. {
  8. Console.WriteLine("学习.net高级班公开课");
  9. }
  10.  
  11. public void StudyAdvanced(string name)
  12. {
  13. Console.WriteLine("学习.net高级班公开课");
  14. }
  15.  
  16. public static void Show()
  17. {
  18. Console.WriteLine(");
  19. }
  20. }

Student

2. 声明委托

  1. public delegate void NoReturnNoPara();
  2. public delegate void NoReturnWithPara(int x, int y);
  3. public delegate int WithReturnNoPara();
  4. public delegate string WithReturnWithPara(int x, int y);

3. =+ 的方法

  1. private void DoNothing()
  2. {
  3. Console.WriteLine("这里是DoNothing");
  4. }
  5.  
  6. private static void DoNothingStatic()
  7. {
  8. Console.WriteLine("这里是DoNothing");
  9. }
  10.  
  11. private void ShowPlus(int x, int y)
  12. {
  13. Console.WriteLine("ShowPlus x={0} y={1} x+y={2}", x, y, x + y);
  14. }
  15.  
  16. private static void ShowPlusStatic(int x, int y)
  17. {
  18. Console.WriteLine("ShowPlusStatic x={0} y={1} x+y={2}", x, y, x + y);
  19. }

4.委托三部曲

声明(在上面)+实例化+调用

  1. {
  2. NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);//2 委托的实例化
  3. method.Invoke();//3 委托调用
  4. method();//另一种调用方式
  5. //method.BeginInvoke(null, null);//异步调用
  6. this.DoNothing();//直接调用对应的方法,效果一样
  7. }

5.多播委托

  1. {
  2. NoReturnNoPara method = new NoReturnNoPara(MyDelegate.DoNothingStatic);
  3. method += student.Study;
  4. method -= student2.Study;
  5. method += this.DoNothing;
  6. method += student.Study;
  7. method += Student.Show;
  8. method += () => Console.WriteLine("这里lambda");
  9. method.Invoke();
  10. //method.BeginInvoke(null,null);//多播委托不能异步
  11. method -= MyDelegate.DoNothingStatic;
  12. method -= MyDelegate.DoNothingStatic;
  13. method -= this.DoNothing;
  14. method -= student.Study;
  15. method -= Student.Show;
  16. method -= () => Console.WriteLine("这里lambda");
  17.  
  18. //委托里面lambda表达式之后加入的方法都无效了?
  19. method.Invoke();
  20. }
  21. {
  22. NoReturnNoPara method = new NoReturnNoPara(student.Study);
  23. method.Invoke();
  24. }
  25. {
  26. NoReturnNoPara method = new NoReturnNoPara(Student.Show);
  27. method.Invoke();
  28. }
  29. {
  30. NoReturnWithPara method = new NoReturnWithPara(ShowPlus);
  31. method += ShowPlus;
  32. method += ShowPlus;
  33. method += ShowPlus;
  34. method -= ShowPlus;
  35. method -= ShowPlus;
  36. method(, );
  37. }

Event

  1. public class Baby
  2. {
  3. public void Cry()
  4. {
  5. Console.WriteLine("{0} Cry", this.GetType().Name);
  6. }
  7. }

Baby

  1. public class Brother
  2. {
  3. public void Turn()
  4. {
  5. Console.WriteLine("{0} Turn", this.GetType().Name);
  6. }
  7. }

Brother

  1. public class Dog
  2. {
  3. public void Wang()
  4. {
  5. Console.WriteLine("{0} Wang", this.GetType().Name);
  6. }
  7. }

Dog

  1. public class Father
  2. {
  3. public void Shout()
  4. {
  5. Console.WriteLine("{0} Shout", this.GetType().Name);
  6. }
  7. }

Father

  1. public class Mother
  2. {
  3. public void Wispher()
  4. {
  5. Console.WriteLine("{0} Wispher", this.GetType().Name);
  6. }
  7. }

Mother

  1. public class Mouse
  2. {
  3. public void Run()
  4. {
  5. Console.WriteLine("{0} Run", this.GetType().Name);
  6. }
  7. }

Mouse

  1. public class Neighbor
  2. {
  3. public void Awake()
  4. {
  5. Console.WriteLine("{0} Awake", this.GetType().Name);
  6. }
  7. }

Neighbor

  1. public class Stealer
  2. {
  3. public void Hide()
  4. {
  5. Console.WriteLine("{0} Hide", this.GetType().Name);
  6. }
  7. }

Stealer

                              发布
  1. /// <summary>
  2. /// 发布者
  3. /// 一只猫,miao一声
  4. /// 导致一系列的触发动作
  5. /// </summary>
  6. public class Cat
  7. {
  8. public void Miao()
  9. {
  10. Console.WriteLine("{0} Miao", this.GetType().Name);
  11. new Neighbor().Awake();
  12. new Mouse().Run();
  13. new Dog().Wang();
  14. new Baby().Cry();
  15. new Mother().Wispher();
  16. new Brother().Turn();
  17. new Father().Shout();
  18. }
  19.  
  20. public Action MiaoAction;//委托的实例
  21. public event Action MiaoActionEvent;//事件是 委托的实例,加了event关键字-----发布者
  22. //event做了权限控制,保证外部不能赋值和调用
  23. public void MiaoDelegate()
  24. {
  25. Console.WriteLine("{0} MiaoDelegate", this.GetType().Name);
  26.  
  27. MiaoAction.Invoke();
  28. }
  29.  
  30. public void MiaoEvent()
  31. {
  32. Console.WriteLine("{0} MiaoEvent", this.GetType().Name);
  33. if (MiaoActionEvent != null)
  34. {
  35. //MiaoActionEvent.Invoke();
  36. //MiaoActionEvent();
  37. foreach (Action item in MiaoActionEvent.GetInvocationList())//触发
  38. {
  39. Console.WriteLine("*********");
  40. item.Invoke();
  41. //item.BeginInvoke(null,null);
  42. }
  43. }
  44. }
  45. }

订阅:

  1.           {
  2. Console.WriteLine("*******Miao******");
  3. Cat cat = new Cat();
  4. cat.Miao();
  5.  
  6. Console.WriteLine("*******MiaoDelegate******");
  7. //cat.MiaoAction += new Neighbor().Awake;
  8.  
  9. cat.MiaoAction += new Mouse().Run;
  10. cat.MiaoAction -= new Mouse().Run;
  11. cat.MiaoAction += new Baby().Cry;
  12. cat.MiaoAction += new Mother().Wispher;
  13.  
  14. cat.MiaoAction.Invoke();
  15. cat.MiaoAction = null;
  16.  
  17. cat.MiaoAction += new Brother().Turn;
  18. cat.MiaoAction += new Father().Shout;
  19. cat.MiaoAction += new Dog().Wang;
  20.  
  21. cat.MiaoDelegate();
  22.  
  23. Console.WriteLine("*******MiaoEvent******");
  24. cat.MiaoActionEvent += new Neighbor().Awake;
  25. cat.MiaoActionEvent += new Mouse().Run;
  26. cat.MiaoActionEvent += new Baby().Cry;
  27.  
  28. //cat.MiaoActionEvent.Invoke();
  29. //cat.MiaoActionEvent = null;
  30.  
  31. cat.MiaoActionEvent += new Mother().Wispher;
  32. cat.MiaoActionEvent += new Brother().Turn;
  33. cat.MiaoActionEvent += new Father().Shout;
  34. cat.MiaoActionEvent += new Dog().Wang;
  35. cat.MiaoEvent();
  36. }

Delegate&Event的更多相关文章

  1. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

  2. [UE4] C++实现Delegate Event实例(例子、example、sample)

    转自:http://aigo.iteye.com/blog/2301010 虽然官方doc上说Event的Binding方式跟Multi-Cast用法完全一样,Multi-Cast论坛上也有很多例子, ...

  3. [C#] Delegate, Multicase delegate, Event

    声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...

  4. C# delegate & event

    public delegate void MyDelegate(string mydelegate);//声明一个delegate对象 //实现有相同参数和返回值的函数        public v ...

  5. Delegate & Event

    Long time without coding,貌似对programming都失去了曾有的一点点sense了,今日有空再细瞄一下.net的委托和事件. Delegate 首先,委托用于引用一类具有相 ...

  6. Delegate event 委托事件---两个From窗体使用委托事件

    窗体如下:   public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...

  7. 委托与事件--delegate&&event

    委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...

  8. delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的

    public void TestF() { Test += Fun; } public void Fun(Person p) { }  // 如 Person变成 SubPerson,则报错..pub ...

  9. ue4 delegate event

    官网相关 https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Delegates/index.html wi ...

随机推荐

  1. usr/bin/ld: cannot find 错误解决方法

    参考:http://blog.siyebocai.cn/20100324_5p424qs7.html 通常在软件编译时出现的usr/bin/ld: cannot find -lxxx的错误,主要的原因 ...

  2. stm32的DFU使用方法

    stm32的dfu看上去是个很高级的东西,似乎可以通过USB给内部flash.外部spi flash.外部nor等东西刷写数据.把数据读出来,但是用了一下感觉确实有点麻烦. 先不管原理是怎样的,使用方 ...

  3. Java模拟网站登录02【转载】

    如何用Java代码模拟一些如百度.QQ之类的网站登录?有两个方式,一是发送模拟请求,二是模拟浏览器操作,而这两种方式恰好在Java有开源实现,在这里介绍一个工具包,它是家喻户晓的HttpClient. ...

  4. BZOJ 2152: 聪聪可可 点分治

    2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...

  5. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  6. [Angular 2] More on *ngFor, @ContentChildren & QueryList<>

    In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...

  7. mysql用变量存储插入的id

    INSERT into a(value) values ('test');#set @last_id = LAST_INSERT_ID();set @last_id = (select max(id) ...

  8. android:layout_weight总有你不知道的用法.

    都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中. 下面说说它的几种用法(以下例子全为横排 注意android:lay ...

  9. VC6.0代码移植到VS2008运行时乱码问题解决

    转载:http://blog.sina.com.cn/s/blog_6d0cbb030101a3cs.html 问题描述:     之前用VC6.0写过一个OpenGL的程序,后来需要将其放到VS20 ...

  10. JMeter 学习笔记从不懂慢慢提升(01)

    开源已经成为一个趋势,虽然说做测试是一个低端的行业,但是我们也应该在这个低端的行业慢慢提升自己,让自己到达理想的高度. 以前说如果你会使用loadrunner可能别人就会觉得你有一定的水平那么就会拿高 ...