C#如何发布符合 .NET Framework 准则的事件
来源:互联网 发布:linux shell 启动程序 编辑:程序博客网 时间:2024/05/18 10:39
最近项目需要,需要用到.NET框架,C#,以前没有接触过,勉强学学,感觉还行,学习起来不是很难。
最近学校“事件”,其中经典的就是pub-sub模型,在.NET中,有一种方法叫做委托,关键字delegate,这种方式有点类似于接口,但是比借口更加灵活,因为delegate支持广播效果,重载了很多操作父,比如+=,+,-=等,操作起来非常方便。
在pub中,声明一个delegate handler,定义好签名(也就是参数设置),在sub中,我们把pub作为参数传递给sub,并且sub自己主动向pub的handler注册自己的方法(这里可以理解为C中的回掉函数),这样pub需要广播时,直接调用handler就行,而不需要关注是谁注册了handler。
其中C#中EventHandler就是一个delegate,在pub创建一个EventHandler属性,sub中给这个属性赋值就行。
代码如下:
namespace DotNetEvents{ using System; using System.Collections.Generic; // Define a class to hold custom event info public class CustomEventArgs : EventArgs { public CustomEventArgs(string s) { message = s; } private string message; public string Message { get { return message; } set { message = value; } } } // Class that publishes an event class Publisher { // Declare the event using EventHandler<T> public event EventHandler<CustomEventArgs> RaiseCustomEvent; public void DoSomething() { // Write some code that does something useful here // then raise the event. You can also raise an event // before you execute a block of code. OnRaiseCustomEvent(new CustomEventArgs("Did something")); } // Wrap event invocations inside a protected virtual method // to allow derived classes to override the event invocation behavior protected virtual void OnRaiseCustomEvent(CustomEventArgs e) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. EventHandler<CustomEventArgs> handler = RaiseCustomEvent; // Event will be null if there are no subscribers if (handler != null) { // Format the string to send inside the CustomEventArgs parameter e.Message += String.Format(" at {0}", DateTime.Now.ToString()); // Use the () operator to raise the event. handler(this, e); } } } //Class that subscribes to an event class Subscriber { private string id; public Subscriber(string ID, Publisher pub) { id = ID; // Subscribe to the event using C# 2.0 syntax pub.RaiseCustomEvent += HandleCustomEvent; } // Define what actions to take when the event is raised. void HandleCustomEvent(object sender, CustomEventArgs e) { Console.WriteLine(id + " received this message: {0}", e.Message); } } class Program { static void Main(string[] args) { Publisher pub = new Publisher(); Subscriber sub1 = new Subscriber("sub1", pub); Subscriber sub2 = new Subscriber("sub2", pub); // Call the method that raises the event. pub.DoSomething(); // Keep the console window open Console.WriteLine("Press Enter to close this window."); Console.ReadLine(); } }}
- C#如何发布符合 .NET Framework 准则的事件
- C#事件之 发布符合 .NET Framework 准则的事件
- 如何:发布符合 .NET Framework 准则的事件(C# 编程指南)
- .NET Framework事件设计准则
- C#事件-设计准则
- c# 如何求出符合条件的总条数
- c#中的事件设计准则
- 如何去发布C# 编写的 ASP.net
- .NET、.NET FrameWork和C#的一知半解
- NET Framework 3.0发布
- C# @符合的作用
- .NET .NET Framework C#
- .NET Framework中的事件
- 如何在没有安装.NET Framework的电脑上运行C#程序
- .Net 以WebBrowser.DocumentCompleted 事件为例,说明用C#如何完成事件的订阅处理
- Microsoft .NET Framework 3.0发布!!
- jspx.Net Framework 1.4 发布
- Jspx.net Framework 5.1 发布
- 在Office自动化条件下读文档属性
- OpenTLD有关
- 无法掩饰的憔悴,无法抗拒的痛紧紧相随
- 程序员英语12--12天结束单词学习?
- 一个阿里巴巴码农的六年回眸
- C#如何发布符合 .NET Framework 准则的事件
- JSP中的input type
- nginx设置emlog伪静态
- PAIP.java程序调试的注意点及流程总结
- CodeDom类介绍
- android UI进阶之自定义组合控件之一
- SQL Server 2005/2008/2012中应用分布式分区视图
- 百度和谷歌谁更敏感
- matlab6.5版本和matlab7版本以上的区别