Delegate

1.基本类:

     public class Student
     {
         public int Id { get; set; }
         public string Name { get; set; }

         public void Study()
         {
             Console.WriteLine("学习.net高级班公开课");
         }

         public void StudyAdvanced(string name)
         {
             Console.WriteLine("学习.net高级班公开课");
         }

         public static void Show()
         {
             Console.WriteLine(");
         }
     }

Student

2. 声明委托

         public delegate void NoReturnNoPara();
         public delegate void NoReturnWithPara(int x, int y);
         public delegate int WithReturnNoPara();
         public delegate string WithReturnWithPara(int x, int y);    

3. =+ 的方法

         private void DoNothing()
         {
             Console.WriteLine("这里是DoNothing");
         }

         private static void DoNothingStatic()
         {
             Console.WriteLine("这里是DoNothing");
         }

         private void ShowPlus(int x, int y)
         {
             Console.WriteLine("ShowPlus x={0} y={1} x+y={2}", x, y, x + y);
         }

         private static void ShowPlusStatic(int x, int y)
         {
             Console.WriteLine("ShowPlusStatic x={0} y={1} x+y={2}", x, y, x + y);
         }    

4.委托三部曲

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

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

5.多播委托

          {
                 NoReturnNoPara method = new NoReturnNoPara(MyDelegate.DoNothingStatic);
                 method += student.Study;
                 method -= student2.Study;
                 method += this.DoNothing;
                 method += student.Study;
                 method += Student.Show;
                 method += () => Console.WriteLine("这里lambda");
                 method.Invoke();
                 //method.BeginInvoke(null,null);//多播委托不能异步
                 method -= MyDelegate.DoNothingStatic;
                 method -= MyDelegate.DoNothingStatic;
                 method -= this.DoNothing;
                 method -= student.Study;
                 method -= Student.Show;
                 method -= () => Console.WriteLine("这里lambda");

                 //委托里面lambda表达式之后加入的方法都无效了?
                 method.Invoke();
             }
             {
                 NoReturnNoPara method = new NoReturnNoPara(student.Study);
                 method.Invoke();
             }
             {
                 NoReturnNoPara method = new NoReturnNoPara(Student.Show);
                 method.Invoke();
             }
             {
                 NoReturnWithPara method = new NoReturnWithPara(ShowPlus);
                 method += ShowPlus;
                 method += ShowPlus;
                 method += ShowPlus;
                 method -= ShowPlus;
                 method -= ShowPlus;
                 method(, );
             }    

Event

    public class Baby
     {
         public void Cry()
         {
             Console.WriteLine("{0} Cry", this.GetType().Name);
         }
     }

Baby

      public class Brother
     {
         public void Turn()
         {
             Console.WriteLine("{0} Turn", this.GetType().Name);
         }
     }

Brother

     public class Dog
     {
         public void Wang()
         {
             Console.WriteLine("{0} Wang", this.GetType().Name);
         }
     }

Dog

     public class Father
     {
         public void Shout()
         {
             Console.WriteLine("{0} Shout", this.GetType().Name);
         }
     }

Father

     public class Mother
     {
         public void Wispher()
         {
             Console.WriteLine("{0} Wispher", this.GetType().Name);
         }
     }

Mother

     public class Mouse
     {
         public void Run()
         {
             Console.WriteLine("{0} Run", this.GetType().Name);
         }
     }

Mouse

     public class Neighbor
     {
         public void Awake()
         {
             Console.WriteLine("{0} Awake", this.GetType().Name);
         }
     }

Neighbor

     public class Stealer
     {
         public void Hide()
         {
             Console.WriteLine("{0} Hide", this.GetType().Name);
         }
     }

Stealer

                              发布
     /// <summary>
     /// 发布者
     /// 一只猫,miao一声
     /// 导致一系列的触发动作
     /// </summary>
     public class Cat
     {
         public void Miao()
         {
             Console.WriteLine("{0} Miao", this.GetType().Name);
             new Neighbor().Awake();
             new Mouse().Run();
             new Dog().Wang();
             new Baby().Cry();
             new Mother().Wispher();
             new Brother().Turn();
             new Father().Shout();
         }

         public Action MiaoAction;//委托的实例
         public event Action MiaoActionEvent;//事件是 委托的实例,加了event关键字-----发布者
         //event做了权限控制,保证外部不能赋值和调用
         public void MiaoDelegate()
         {
             Console.WriteLine("{0} MiaoDelegate", this.GetType().Name);

             MiaoAction.Invoke();
         }

         public void MiaoEvent()
         {
             Console.WriteLine("{0} MiaoEvent", this.GetType().Name);
             if (MiaoActionEvent != null)
             {
                 //MiaoActionEvent.Invoke();
                 //MiaoActionEvent();
                 foreach (Action item in MiaoActionEvent.GetInvocationList())//触发
                 {
                     Console.WriteLine("*********");
                     item.Invoke();
                     //item.BeginInvoke(null,null);
                 }
             }
         }
     }

订阅:

           {
                     Console.WriteLine("*******Miao******");
                     Cat cat = new Cat();
                     cat.Miao();

                     Console.WriteLine("*******MiaoDelegate******");
                     //cat.MiaoAction += new Neighbor().Awake;

                     cat.MiaoAction += new Mouse().Run;
                     cat.MiaoAction -= new Mouse().Run;
                     cat.MiaoAction += new Baby().Cry;
                     cat.MiaoAction += new Mother().Wispher;

                     cat.MiaoAction.Invoke();
                     cat.MiaoAction = null;

                     cat.MiaoAction += new Brother().Turn;
                     cat.MiaoAction += new Father().Shout;
                     cat.MiaoAction += new Dog().Wang;

                     cat.MiaoDelegate();

                     Console.WriteLine("*******MiaoEvent******");
                     cat.MiaoActionEvent += new Neighbor().Awake;
                     cat.MiaoActionEvent += new Mouse().Run;
                     cat.MiaoActionEvent += new Baby().Cry;

                     //cat.MiaoActionEvent.Invoke();
                     //cat.MiaoActionEvent = null;

                     cat.MiaoActionEvent += new Mother().Wispher;
                     cat.MiaoActionEvent += new Brother().Turn;
                     cat.MiaoActionEvent += new Father().Shout;
                     cat.MiaoActionEvent += new Dog().Wang;
                     cat.MiaoEvent();
                 }

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. 用Spring3编写第一个HelloWorld项目

    第一个HelloWorld程序 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclipse IDE的操作 如果你还没有设置好环境 ...

  2. EasyUI基础入门之Parser(解析器)

    前言 JQuery EasyUI提供的组件包含功能强大的DataGrid,TreeGrid.面板.下拉组合等.用户能够组合使用这些组件,也能够单独使用当中一个.(使用的形式是以插件的方式提供的) Ea ...

  3. C# 中的 lock的陷阱

    旧事重提了,或许很多人会奇怪,为什么 C# 不允许lock一个struct ? 例如: public void ProcessTask(int taskid){     lock(taskid){  ...

  4. 用C#调用Matlab图像处理自制QQ游戏2D桌球瞄准器

    平时不怎么玩游戏,有时消遣就玩玩QQ里的2D桌球,但是玩的次数少,不能像骨灰级玩家一样百发百中,肿么办呢?于是某天突发奇想,决定自己也来做个“外挂”.说是外挂,其实只是一个瞄准器,毕竟外挂是修改别人的 ...

  5. 第二周02:Fusion ICP逐帧融合

    本周主要任务02:Fusion 使用ICP进行逐帧融合 任务时间: 2014年9月8日-2014年9月14日 任务完成情况: 已实现将各帧融合到统一的第一帧所定义的摄像机坐标系下,但是由于部分帧之间的 ...

  6. [AngularJS] Introduction to ui-router

    Introduce to basic $stateProvider.state() with $stateParams services. Understand how nested router w ...

  7. JavaEE系列之(一)JSP基础知识详解

    一.JSP基础语法     1.JSP简介        JSP(Java Server Pages),其根本是一个简化的Servlet设计,它实现了在Java中使用HTML标签.JSP是一种动态网页 ...

  8. android UI进阶之实现listview的分页加载

    上篇博文和大家分享了下拉刷新,这是一个用户体验非常好的操作方式.新浪微薄就是使用这种方式的典型. 还有个问题,当用户从网络上读取微薄的时候,如果一 下子全部加载用户未读的微薄这将耗费比较长的时间,造成 ...

  9. 【javaSE】HashSet和HashMap

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  10. 分享一个jQuery动态网格布局插件:Masonry(转)

    在线演示 Masonry是 一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发类似剪贴画的界面效果.和CSS中float的效果不太一样的地方在 于,float先水平排列,然后再垂直排 ...