C#通用事件处理程序与非通用 [英] C# Generic Event Handlers vs Non Generic

查看:105
本文介绍了C#通用事件处理程序与非通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新考虑C#和oop几年后编程PLC。我正在通过一些例子阅读代表和活动。我想知道使用通用事件处理程序比非通用事件处理程序有什么好处?例如,使用的好处是什么:

公共事件EventHandler< MyNewEventArgs> SomethingHappened 



(其中MyNewEventArgs是EventArgs的基类



over

  public   event  EventHandler SomethingHappened 



我在互联网上搜索过但没有找到令人满意的解释。



先谢谢

解决方案

事件处理程序的签名通常是:

  void  HandleThis( object  sender,MyEventErgs args); 



您需要为此创建一个委托才能将其用于事件:

  public   delegate  MyEventHandler( object  sender,MyEventErgs args); 



您的事件声明如下:

< pre lang =c#> public event MyEventHandler MyEvent;



想象一下你有很多不同事件,即具有不同参数的事件。使用通用事件暂停将为您节省声明委托的工作。您可以在处理方法保持不变的情况下对此事件进行详细说明:

  public   event  EventHandler< myeventargs> MyEvent; 
< / myeventargs >


使用< T>的Generic'事件委托类型(自.NET 2.0起可用)其中'T 可能继承自(其基类为)的类型当您需要将自定义数据从触发事件的上下文传递给事件的使用者时,使用EventArgs谁为事件创建EventHandlers并将这些EventHandler添加到事件的调用列表中。



请注意,我说''T可能是:这不是绝对必需的:您可以使用以下方法:

  public   class  MyNewEventArgs  //  注意:不从EventArgs继承 
{
public int someX = 199 ;

MyNewEventArgs( int x = 999
{
someX = x;
}
}

public event EventHandler< myneweventargs>发生了一些事;

// 订阅活动
private void Form1_Load( object sender,EventArgs e)
{
SomethingHappened + = Form1_SomethingHappened;
}

// EventHandler
public void Form1_SomethingHappened( object sender,Form1.MyNewEventArgs e)
{
Console.WriteLine(e.someX);
}

// 按下按钮点击事件
private void button1_Click( object sender,EventArgs e)
{
SomethingHappened( this new MyNewEventArgs(< span class =code-digit> 1001 ));
} < / myneweventargs >

使用'Action和'Func委托(他们是完全成熟的多播生物),您可以使用自定义参数获得更简洁的等效通用EventHandler,但通常最好坚持使用使用继承自'EventArgs的自定义类的经典格式:产生更一致的代码,更容易维护代码,以及代码的外部消费者发现符合预期的代码。



这里最近对另一个问题的回答[ ^ ]我举了一个在通用事件定义中使用基于Custom EventArgs的类的例子。



该示例着重于如何使用实现接口的通用事件,但我想你你可以在查看这个例子以及我对成员关于该例子的问题的评论中找到一些价值。


I'm currently revisiting C# and oop after a few years programming PLCs. I'm reading up on delegates and events working through a few examples. I was wondering what are the advantages of using generic event handlers over non-generic event handlers? for example,what is the benefit of using:

public event EventHandler<MyNewEventArgs> SomethingHappened


(where MyNewEventArgs is a base class of EventArgs

over

public event EventHandler SomethingHappened


I've searched on the internet and not found a satisfactory explanation.

Thanks in Advance

解决方案

Signature of an event handler is usually this:

void HandleThis(object sender, MyEventErgs args);


You need to create a delegate for that in order to use it with an event:

public delegate MyEventHandler(object sender, MyEventErgs args);


And your event declaration is this:

public event MyEventHandler MyEvent;


Imagine you have lots of different events, i.e. events with different arguments. Using a generic event halder will save you the work of declaring the delegate. You can delcare your event like this while your handler method stays the same:

public event EventHandler<myeventargs> MyEvent;
</myeventargs>


The Generic 'event delegate Type (available since .NET 2.0) that uses <T> where 'T may be a Type that inherits from (whose base class is) EventArgs is used when you need to pass custom data from the context in which the Event is triggered to the "consumers" of the Event who create EventHandlers for the Event and add those EventHandlers to the Invocation List of the Event.

Note that I said "'T may be:" that's not absolutely required: you can get away with using this:

public class MyNewEventArgs // note: does not inherit from EventArgs
{
    public int someX = 199;

    MyNewEventArgs(int x = 999)
    {
      someX = x;
    }
}

public event EventHandler<myneweventargs> SomethingHappened;

// subscribe to the Event
private void Form1_Load(object sender, EventArgs e)
{
    SomethingHappened += Form1_SomethingHappened;
}

// EventHandler
public void Form1_SomethingHappened(object sender, Form1.MyNewEventArgs e)
{
   Console.WriteLine(e.someX);
}

// call the Event on button click
private void button1_Click(object sender, EventArgs e)
{
    SomethingHappened(this, new MyNewEventArgs(1001));
}</myneweventargs>

You could get even more concise equivalents to a generic EventHandler with custom arguments using 'Action and 'Func delegates (they are full-fledged multi-cast creatures), but generally it's always better to stick to the "classic" format of using a custom class that inherits from 'EventArgs: results in more consistent code, easier to maintain code, and code that external "consumers" of your code finds consistent with their expectations.

In a recent answer to another question here [^] I gave an example of using a Custom EventArgs based class in a generic Event definition.

That example focuses on how to use a generic Event that implements an Interface, but I think you may find some value in looking at that example and at my comments to a question from a member about that example.


这篇关于C#通用事件处理程序与非通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆