C#中简单的事件提高 - 使用"发送方QUOT;与定制的EventArgs [英] C# simple Event Raising - using "sender" vs. custom EventArgs

查看:111
本文介绍了C#中简单的事件提高 - 使用"发送方QUOT;与定制的EventArgs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这种情况。我有一个对象,让我们叫它....美孚。富提出了一个名为装一个简单的事件。由于信息的事件时,消费者需要知道哪些Foo对象引发的事件。我们的团队采取了以下模式

Consider this scenario. I have an object, lets call it.... Foo. Foo raises a simple event named "Loaded". As part of the information for the event, consumers will need to know which foo object raised the event. Our team has adopted the following pattern.

1)创建一个继承于EventArgs的新类 - 例如,
FooEventArgs:System.EventArgs

1) Create a new class that inherits from EventArgs - for example, FooEventArgs : System.EventArgs.

2),Foo类型的属性添加到FooEventArgs,这是通过构造函数传递的设置。

2) Add a property of type Foo to the FooEventArgs, which is set by being passed in via the constructor.

3)使用事件处理程序的通用版本声明事件,因此

3) Declare the event using the generic version of EventHandler, so

public event EventHandler<FooEventArgs> Loaded;



4)提高从Foo类具有以下签名的事件:

4) Raise the event from the Foo class with the following signature:

Loaded(this, new FooEventArgs(this));

从本质上讲这是什么做的是使得发件人foo的对象,但它的的把foo的对象引用到事件参数作为一个强类型属性。

Essentially what this does is makes the "sender" the foo object, but it also puts the foo object reference into the event argument as a strongly typed property.

这样做的一个好处是,没有一个人有铸造打扰发件人,当他们处理该事件,从而降低事件消费者和事件养殖者之间的耦合。另一种优势在于,如果事件专业户类型不断有变化,因此强类型属性(但愿不会发生),则不是仅仅拥有代码开始就投失败出来时为空,该API实际上打破,因此它可以被固定在编译的时候。

One advantage for doing this is that no one has to bother with casting "sender" when they handle the event, which lowers the coupling between the event consumer and the event raiser. Another "advantage" is that if the type of the event raiser ever has to change, and hence the strongly typed property (which hopefully never happens), then instead of simply having code start to fail on the cast when it comes out as null, the API actually breaks so it can be fixed at compile time.

在我看来,这种模式似乎是它可能是矫枉过正。他们应该被信任的发件人参数多,开沟自定义事件参数?我的团队认为,没有人真正使用发件人参数。 ?什么是用于传递出事件的宣传对象的引用的最佳实践

To me, this pattern seems like it might be overkill. Should they be trusting the "sender" parameter more, and ditching the custom event arguments? My team argues that no one really uses the sender parameter. What's the best practice for passing out a reference to the event-raising object?

编辑:伟大的反馈到目前为止,我将离开这个开放的另一天左右之前,我接受一个。

推荐答案

常见的模式是利用发送者,而不是单独添加发件人EventArgs的。自定义的EventArgs用于像一棵树的事件,一个(可设置)布尔为可取消的事件等树节点等状态。

The common pattern is to use sender and not add the sender separately to the EventArgs. The custom EventArgs is used for other state like a tree node for a tree event, a (settable) boolean for a cancelable event etc.

我们使用常见的模式,因为它由BCL类也使用,用于制作自制事件的差异可能造成混乱。另外,我们有一个全球性的IoC发行者 - 订阅模式,但这个只能用正常的事件处理程序委托签名的作品,因为类型事先并不知道。在这种情况下,流延(例如到自定义EventArgs的)是必要的反正,所以我们还不如投发件人

We use the common pattern since it is used by the BCL classes also, and making a difference for "self-made events" is potentially confusing. Also, we have a global IoC publisher-subscriber pattern but this one only works with the normal EventHandler delegate signature, since the types are not known in advance. In this case, a cast (for instance to a custom EventArgs) is necessary anyways, so we might just as well cast the sender.

这篇关于C#中简单的事件提高 - 使用&QUOT;发送方QUOT;与定制的EventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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