问题描述
我目前正在C#中使用delagate进行硬体理解和实现活动。我习惯了Java的做事方式:- 定义一个监听器类型的接口,该接口将包含多个方法定义
-
- 为该界面定义适配器类,如果我对所有在监听器中定义的事件不感兴趣,可以使事情变得更容易。
- 定义添加,删除和获取引发事件的类中的[]方法
- 定义受保护的fire方法来循环添加侦听器列表并调用正确的方法
这个我明白(和!!) - 我知道我可以在c#中完全一样,但是似乎一个新的(更好的)系统是c#的地方。阅读无数的教程,解释在c#中使用代表和事件,我仍然没有更接近真正了解发生了什么:S
简而言之,对于以下方法,我将如何在c#中实现事件系统:
void computerStarted(Computer computer) ;
void computerStopped(Computer computer);
void computerReset(Computer computer);
void computerError(计算机计算机,异常错误);
^以上方法取自我曾经做过的Java应用程序,我试图移植到c#。
很多谢谢!
创建四个事件和方法来提高它们,以及一个新的基于EventArgs的类来指示错误:
public class ExceptionEventArgs :EventArgs
{
private readonly异常错误;
public ExceptionEventArgs(异常错误)
{
this.error = error;
}
public错误
{
get {return error;
}
}
public class Computer
{
public event EventHandler Started = delegate {};
public event EventHandler Stopped = delegate {};
public event EventHandler Reset = delegate {};
public event EventHandler< ExceptionEventArgs> Error = delegate {};
protected void OnStarted()
{
Started(this,EventArgs.Empty);
}
protected void OnStopped()
{
已停止(此为EventArgs.Empty);
}
protected void OnReset()
{
重置(this,EventArgs.Empty);
}
protected void OnError(异常e)
{
错误(这是新的ExceptionEventArgs(e));
}
}
然后类可以使用方法订阅事件或一个匿名函数:
someComputer.Started + = StartEventHandler; //方法
someComputer.Stopped + = delegate(object o,EventArgs e)
{
Console.WriteLine({0} has started,o);
};
someComputer.Reset + =(o,e)=> Console.WriteLine({0}已被重置);
需要注意的几件事情是:
- OnXXX方法受到保护,以便派生类可以引发事件。这并不总是必要的 - 按照你的看法做到这一点。
- 每个事件声明的
委托{}
避免不必执行空检查的技巧。它正在为每个事件订阅一个无操作的事件处理程序。 - 事件声明是类似字段的事件。实际创建的是变量和事件。在课堂里你看到变量;
请参阅我的文章更多的事件细节。
I am currently having a hardtime understanding and implementing events in C# using delagates. I am used to the Java way of doing things:
- Define an interface for a listener type which would contain a number of method definitions
- Define adapter class for that interface to make things easier if I'm not interested in all the events defined in a listener
- Define Add, Remove and Get[] methods in the class which raises the events
- Define protected fire methods to do the dirty work of looping through the list of added listeners and calling the correct method
This I understand (and like!) - I know I could do this exactly the same in c#, but it seems that a new (better?) system is in place for c#. After reading countless tutorials explaining the use of delegates and events in c# I still am no closer to really understanding what is going on :S
In short, for the following methods how would I implement the event system in c#:
void computerStarted(Computer computer);
void computerStopped(Computer computer);
void computerReset(Computer computer);
void computerError(Computer computer, Exception error);
^ The above methods are taken from a Java application I once made which I'm trying to port over to c#.
Many many thanks!
You'd create four events, and methods to raise them, along with a new EventArgs-based class to indicate the error:
public class ExceptionEventArgs : EventArgs
{
private readonly Exception error;
public ExceptionEventArgs(Exception error)
{
this.error = error;
}
public Error
{
get { return error; }
}
}
public class Computer
{
public event EventHandler Started = delegate{};
public event EventHandler Stopped = delegate{};
public event EventHandler Reset = delegate{};
public event EventHandler<ExceptionEventArgs> Error = delegate{};
protected void OnStarted()
{
Started(this, EventArgs.Empty);
}
protected void OnStopped()
{
Stopped(this, EventArgs.Empty);
}
protected void OnReset()
{
Reset(this, EventArgs.Empty);
}
protected void OnError(Exception e)
{
Error(this, new ExceptionEventArgs(e));
}
}
Classes would then subscribe to the event using either a method or a an anonymous function:
someComputer.Started += StartEventHandler; // A method
someComputer.Stopped += delegate(object o, EventArgs e)
{
Console.WriteLine("{0} has started", o);
};
someComputer.Reset += (o, e) => Console.WriteLine("{0} has been reset");
A few things to note about the above:
- The OnXXX methods are protected so that derived classes can raise the events. This isn't always necessary - do it as you see fit.
- The
delegate{}
piece on each event declaration is just a trick to avoid having to do a null check. It's subscribing a no-op event handler to each event - The event declarations are field-like events. What's actually being created is both a variable and an event. Inside the class you see the variable; outside the class you see the event.
See my events/delegates article for much more detail on events.
这篇关于C#事件处理(与Java相比)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!