.NET事件处理器 - 通用还是没有? [英] .NET EventHandlers - Generic or no?

查看:165
本文介绍了.NET事件处理器 - 通用还是没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我开始在深C#项目,我结束了很多,真正只需要通过一个单一的项目活动。我坚持使用事件处理程序 / EventArgs的的做法,但我喜欢做的是这样的:

Every time I start in deep in a C# project, I end up with lots of events that really just need to pass a single item. I stick with the EventHandler/EventArgs practice, but what I like to do is have something like:

public delegate void EventHandler<T>(object src, EventArgs<T> args);

public class EventArgs<T>: EventArgs {

  private T item;

  public EventArgs(T item) {
    this.item = item;
  }

  public T Item {
    get { return item; }
  }
}

后来,我也有我的

Later, I can have my

public event EventHandler<Foo> FooChanged;

public event EventHandler<Bar> BarChanged;

不过,似乎对.NET的标准是创建一个新的委托和 EventArgs的子为每种类型的事件。是不是有什么毛病我一般的做法?


编辑:这样做的原因是后,我只是在一个新的项目重新创建这一点,并希望确保它是确定的。其实,我是重新创建它,因为我张贴。我发现有一个通用的事件处理程序&LT; TEventArgs> ,所以你不需要创建通用的委托,但你仍然需要普通的的EventArgs&LT ; T> 类,因为 TEventArgs:EventArgs的
另一个编辑:内置解决方案的一个缺点(对我来说)是额外的详细程度:

However, it seems that the standard for .NET is to create a new delegate and EventArgs subclass for each type of event. Is there something wrong with my generic approach?


The reason for this post is that I just re-created this in a new project, and wanted to make sure it was ok. Actually, I was re-creating it as I posted. I found that there is a generic EventHandler<TEventArgs>, so you don't need to create the generic delegate, but you still need the generic EventArgs<T> class, because TEventArgs: EventArgs.
Another One downside (to me) of the built-in solution is the extra verbosity:

public event EventHandler<EventArgs<Foo>> FooChanged;

VS

public event EventHandler<Foo> FooChanged;

这可以是一个痛苦,为客户注册您的活动,但因为系统命名空间默认情况下进口的,所以他们不得不手动找出您的命名空间,即使像ReSharper的一个奇特的工具...任何人有任何的想法有关呢?

It can be a pain for clients to register for your events though, because the System namespace is imported by default, so they have to manually seek out your namespace, even with a fancy tool like Resharper... Anyone have any ideas pertaining to that?

推荐答案

代表,因为.NET框架已经加入2.0

Delegate of the following form has been added since .NET Framework 2.0

public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs

您接近去远一点,因为你提供了即开即用的现成实现的EventArgs与单个数据项,但它缺乏的最初设想的几个特性:

You approach goes a bit further, since you provide out-of-the-box implementation for EventArgs with single data item, but it lacks several properties of the original idea:

  1. 您不能在不改变依赖code添加更多属性的事件数据。你将不得不改变委托签名,以提供更多的数据,事件订阅。
  2. 您的数据对象是通用的,但它也是匿名,并在阅读code,你将不得不破译从用途项目属性。它应根据它提供的数据来命名。
  3. 使用泛型这样,你不能让EventArgs的并行层次,当你有潜在的(项目)类型的层次结构。例如。 EventArgs的&LT;基本类型&GT;是不是基地类型的EventArgs&LT; D​​erivedType&gt;中,即使基本类型是基地DerivedType。

所以,我觉得这是更好地使用通用的事件处理程序&LT; T&GT ;,但仍然有定制的EventArgs类,根据数据模型的要求举办的。使用Visual Studio和扩展喜欢ReSharper的,它是几个命令只是早晚的事来创建新类这样的。

So, I think it is better to use generic EventHandler<T>, but still have custom EventArgs classes, organized according to the requirements of the data model. With Visual Studio and extensions like ReSharper, it is only a matter of few commands to create new class like that.

这篇关于.NET事件处理器 - 通用还是没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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