在C#中的事件处理程序,为什么一定要在"发送方QUOT;参数是一个对象? [英] In a C# event handler, why must the "sender" parameter be an object?

查看:107
本文介绍了在C#中的事件处理程序,为什么一定要在"发送方QUOT;参数是一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据微软的事件命名指南中,发件人在C#事件处理函数参数是的总是的类型的对象,即使是可以使用更具体的类型。

According to Microsoft event naming guidelines, the sender parameter in a C# event handler "is always of type object, even if it is possible to use a more specific type".

这会导致大量的事件处理code,如:

This leads to lots of event handling code like:

RepeaterItem item = sender as RepeaterItem;
if (item != null) { /* Do some stuff */ }

现在,在我的情况,我与定制的C#事件工作(而不是内置的ASP.NET事件,没有ASP.NET可言到目前为止参与),所以我绝对不用担心意外表演如操作一个GridView的头。

Now, in my case, I'm working with custom C# events (rather than built-in ASP.NET events, no ASP.NET at all involved so far), so I definitely don't have to worry about accidentally performing an operation on e.g. a GridView's Header.

这是说,为什么对一个更具体的类型声明的事件处理程序的约定建议?

That said, why does the convention advise against declaring an event handler with a more specific type?

MyType
{
    public event MyEventHander MyEvent;
}

...

delegate void MyEventHander(MyType sender, MyEventArgs e);

我缺少一个疑难杂症?

Am I missing a gotcha?

此外,它发生,我认为你可以在展开事件处理程序和LT; TArgs>

Also, it occurs to me that you could expand on EventHandler<TArgs> with:

public class EventArgs<TContent> : EventArgs
{
    public TContent Content { get; set; }
}

public delegate void EventHandler<TContent>
    (object sender, EventArgs<TContent> e);

这是否看起来臭?

Does that look smelly?

<子>为后人:我同意的答案一般的情绪,该公约的的使用对象,即使它是可以使用更具体的类型,而在现实世界编程它的的重要遵循的惯例。

For posterity: I agree with the general sentiment in the answers that the convention is to use object even when it is possible to use a more specific type, and in real-world programming it is important to follow the convention.

推荐答案

嗯,这是一个模式,而不是规则。它意味着一个组件可以从另一个事件转发,保持原始发件人即使它不是普通型引发事件。

Well, it's a pattern rather than a rule. It does mean that one component can forward on an event from another, keeping the original sender even if it's not the normal type raising the event.

我同意这是一个有点奇怪 - 但它可能是值得坚持公约只为熟悉的缘故。 (对于熟悉其他开发人员,这是)我从来没有特别热衷于 EventArgs的我自己(因为自身它所传达任何信息),但这是另一个话题。 (至少我们已经得到了事件处理程序和LT; TEventArgs&GT; 现在 - 但如果也有这将有助于一个 EventArgs的&LT; TContent&GT; 为了共同的情况下,你只需要传播的一个值。)

I agree it's a bit strange - but it's probably worth sticking to the convention just for familiarity's sake. (Familiarity for other developers, that is.) I've never been particularly keen on EventArgs myself (given that on its own it conveys no information) but that's another topic. (At least we've got EventHandler<TEventArgs> now - although it would help if there were also an EventArgs<TContent> for the common situation where you just need a single value to be propagated.)

编辑:这确实让代表更通用的,当然 - 一个委托类型可以在多个活动中重用。我不知道我买的是一个特别好的理由 - 尤其是在仿制药的光 - 但我想这是的的东西 ...

It does make the delegate more general purpose, of course - a single delegate type can be reused across multiple events. I'm not sure I buy that as a particularly good reason - particularly in the light of generics - but I guess it's something...

这篇关于在C#中的事件处理程序,为什么一定要在&QUOT;发送方QUOT;参数是一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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