• .NET消息发布和订阅机制的实现案例


    .NET消息发布和订阅机制的实现在被动接受消息中应用很多,下面给出其基本案例:在此案例中,本人还完全测试了中文编程

         基础知识:

          事件基于委托,为委托提供了一种发布/订阅机制。在架构内到处都能看到事件。在W跏s
    应用程序中,Button类提供了Cl硖事件。这类事件就是委托。触发Cl澉事件时调用的处理程序方
    法需要定义,其参数由委托类型定义。
        本案例中,事件用于连接CarDealer类和Co11slmer类。CarDealer类提供了一个新
    车到达时触发的事件。Consuner类订阅该事件,以获得新车到达的通知。

       CarDealer类提供了助mⅡ狲讹κm岣⒕n晚,类型的NewcarMo事件。作为一个约定,
    事件一般使用带两个参数的方法,其中第一个参数是一个对象,包含事件的发送者,第二个参数提供
    了事件的相关信息。第二个参数随不同的事件类型而不同。.NET1,0为所有不同数据类型的事件定义
    了几百个委托。有了泛型委托Evenmandl-后,这就不再需要委托了。Evg耐妇d¢四hmⅡ蚯g少
    定义了一个处理程序,它返回vom,接受两个参数。对于EvcntIIandl-ventArgρ ,第一个参数必
    须是d冰⒍类型,第二个参数是T类型。EventHandl-ventArg少还定义了一个关于T的约束:它
    必须派生自基类EventArgs,CarWoEventArgs就派生自基类EventArgs:从CarDealer类开始,它基于事件提供一个订阅。CarDealer类用event关键字定义了类型为的NewCarLfo事件。在NewCarO方法中,触发NewcarLfo事件:

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 测试消息订阅机制
    {
        public partial class 发布类 : Form
        {
            public 发布类()
            {
                InitializeComponent();
            }
           
            public event EventHandler<车到事件参数类> 车到事件;
            public bool 车到方法动作(string 车名内部变量)
            {
                bool re=false;
                //textBox1.Text = string.Format("有车来了{0}",车名内部变量);
                MessageBox.Show(string.Format("发布消息:{0}车来了", 车名内部变量));
                if (车到事件 != null)
                {
                    车到事件(thisnew 车到事件参数类(车名内部变量));
                    re=true;
                }
                else
                {
                    MessageBox.Show("车到事件为空!");
                }
                return re;
            }
            监听类 监听对象1;
            private void button1_Click(object sender, EventArgs e)
            {
                监听对象1 = new 监听类("小明");
                监听对象1.Show();


                //var 发布对象 = new 发布类();
                
    //var 监听对象 = new 监听类("小明");
                
    //var 监听对象1 = new 监听类("小红");
                
    //发布对象.车到事件 += 监听对象.车到方法动作;
                
    //this.车到事件 += 监听对象.车到方法动作;

            }

            private void button2_Click(object sender, EventArgs e)
            {
                this.车到事件 += 监听对象1.车到方法动作;

                if (this.车到方法动作("宝马"))
                {
                    this.车到事件 -= 监听对象1.车到方法动作;
                }
            }
        }
        public class 车到事件参数类 : EventArgs
        {

            public string 车名 { getprivate set; }

            public 车到事件参数类(string 车名内部变量)
            {
                this.车名 = 车名内部变量;
            }
        }
        
    }


    事件发布程序
    从CarDealer类开始,它基于事件提供一个订阅。CarDealer类用event关键字定义了类型为NewCarLfo事件。在NewCarO方法中,触发NewcarLfo事件

    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 测试消息订阅机制
    {
        public partial class 监听类 : Form
        {
            public 监听类()
            {
                InitializeComponent();
            }
            private string 发布者;

            public 监听类(string 发布者):base()
            {
                this.发布者 = 发布者;
                InitializeComponent();
            }
            public void 车到方法动作(object sender, 车到事件参数类 e)
            {
                textBox1.Text = string.Format("{0}:车名 {1} is Come ", 发布者, e.车名);
                MessageBox.Show(string.Format("{0}说: {1}车 来了! ", 发布者, e.车名));
            }
            
        }
    }

    附本demo源码/Files/sung/测试消息订阅机制.rar

  • 相关阅读:
    寻找完全数(C++)
    莱布尼兹三角形(C++)
    简单的素数问题(C++)
    ubuntu17.04下安装LNMP
    ubuntu下连接mysql出现Access denied for user 'rose'@'localhost' (using password: NO)的解决方法
    快速理解面向对象的PHP编程--基础篇
    百度电面总结
    操作系统基础知识
    快速理解C语言指针
    新手学习MongoDB的基本命令
  • 原文地址:https://www.cnblogs.com/sung/p/2740415.html
Copyright © 2020-2023  润新知