的.NET活动签名NET

由网友(- 嚼着炫迈做着梦)分享简介:可能重复: Why应在C#中取(发件人,EventArgs)? 事件我只是跑一个项目的VS2012 code分析工具,并发现它抱怨这个片段:公共委托无效PerMbHandler(长totalMb);公共事件PerMbHandler NotifyMegabyteIncrement;CA1009声明MyWebClient...

可能重复:   Why应在C#中取(发件人,EventArgs)?

事件

我只是跑一个项目的VS2012 code分析工具,并发现它抱怨这个片段:

 公共委托无效PerMbHandler(长totalMb);

公共事件PerMbHandler NotifyMegabyteIncrement;
 

  

CA1009声明MyWebClient.PerMbHandler作为一个EventArgs,或扩展EventArgs的一个类型的实例,命名为E的第二个参数。

MSDN解释:

  

事件处理方法需要两个参数。首先是System.Object类型,并命名为发件人。这是引发事件的对象。第二个参数的类型是System.EventArgs,并命名为E。这是与事件相关联的数据。例如,如果当文件被打开的情况下升高时,该事件数据通常包含文件的名称

MSDN各国根本什么约定,而不是它存在的原因。

有什么可以去错用的长的参数,而不是对的的EventArgs 的一个子类?它是对公约和程序员期望的事,或者是有一些微妙的技术原因,该模式必须遵循?我说的微妙,因为code似乎很好地工作。

解决方案   

有什么可以去错用长的参数,而不是一个子类   EventArgs的?

事件参数 这本身并不是错,但它是不可扩展的。为了便于讨论,也许6个月,你需要传递两个多头,或添加一个字符串,或添加信息的完整列表。随着 EventArgs的的签名,你可以通过任何派生类型,而不是破坏现有的消费者;有一个特定的值类型,你是非常有限的。

EventArgs的,您还可以与引发事件的类,如沟通 CancelEventArgs

发件人 说实话,我很少用发送者的任何东西。它也可以是有些模糊。例如,在通过输入触发进入文本框的控制的定制事件...谁是发送方?文本框(可能更有用),或控制该声明自定义事件?

不过,这是一个熟悉的惯例,并配有证据充分的接口也可以是很有用的。

Possible Duplicate: Why should events in C# take (sender, EventArgs)?

对邪教说不 网上签名活动

I just ran the VS2012 Code Analysis tool on a project and found that it complains about this snippet:

public delegate void PerMbHandler(long totalMb);

public event PerMbHandler NotifyMegabyteIncrement;

CA1009 Declare the second parameter of 'MyWebClient.PerMbHandler' as an EventArgs, or an instance of a type that extends EventArgs, named 'e'.

MSDN explains:

Event handler methods take two parameters. The first is of type System.Object and is named 'sender'. This is the object that raised the event. The second parameter is of type System.EventArgs and is named 'e'. This is the data that is associated with the event. For example, if the event is raised whenever a file is opened, the event data typically contains the name of the file.

MSDN states simply what the convention is, not the reason it exists.

What can go wrong using a long parameter rather than a subclass of EventArgs? Is it a matter of convention and programmer expectations, or is there some subtle technical reason that the pattern must be followed? I say subtle, because the code seems to work fine.

解决方案

What can go wrong using a long parameter rather than a subclass of EventArgs?

Event Args It's not wrong per se but it's not extensible. For sake of argument, perhaps in 6 months you need to pass two longs, or add a string, or add a whole list of information. With EventArgs in the signature, you can pass any derived type and not break existing consumers; with a specific value type, you are very limited.

EventArgs also allows you to communicate with the class that raised the event, e.g. CancelEventArgs.

Sender Honestly, I rarely use sender for anything. It can also be somewhat ambiguous. For example, a custom event on a control triggered by input into a textbox...who is the sender? the textbox (probably more useful), or the control which declares the custom event?

Still, it is a familiar convention, and with a well-documented interface it can be useful.

阅读全文

相关推荐

最新文章