什么是有事件符合净指引的好处是什么? [英] What are the benefits of having events conforming to Net guidelines?

查看:102
本文介绍了什么是有事件符合净指引的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何根据Net框架准则使用活动,但什么是使用这种模式的好处是什么?

I understand how to use Events according to Net Framework guidelines, but what are the benefits of using this pattern?

<一个href="http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx">http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

.NET框架的指导方针表明,使用的委托类型   一个事件应采取两个参数,一个对象源参数   指示事件的源,和一个e的参数,它   封装有关该事件的任何其他信息。的类型   在E参数应是来自EventArgs类。对于事件   不使用任何额外的信息,.NET框架具有   已定义一个适当的委托类型:事件处理程序

The .NET Framework guidelines indicate that the delegate type used for an event should take two parameters, an "object source" parameter indicating the source of the event, and an "e" parameter that encapsulates any additional information about the event. The type of the "e" parameter should derive from the EventArgs class. For events that do not use any additional information, the .NET Framework has already defined an appropriate delegate type: EventHandler.

一)我看到在使用对象源的值作为自存在其中多个对象可以有自己的事件设置为相同的方法的情况下的第一参数的一些好处。因此,例如,如果我们有10个对象,如果所有的10个对象设置他们的活动,事件处理男,然后在里面中号,我们可以使用对象发件人参数值,以确定事件调用的始作俑者。

a) I see some benefits in using "object source" value as a first parameter since there are situations where multiple objects can have their events set to the same method. Thus, if for example we have 10 objects and if all 10 objects set their events to event handler M, then inside M we can use "object sender" parameter value to identify the initiator of the event call.

  • 不过,据我所知,对象源参数,如果事件实例方法中提出了才有用。因此,如果活动是在静态方法,那么对象源参数提高是没有用的?

二)是否有使用事件与按照以.Net Framework的指引其他好处?

b) Are there other benefits of using events with accordance to Net Framework guidelines?

三)不管是什么好处可能是,为什么他们会出重不必的麻烦

c) Whatever the benefits may be, why would they out-weight the trouble of having to

  • 在编写额外的code把所需的参数为从EventArgs的派生的对象
  • 在编写额外的code里面的事件处理程序中提取从EventArgs的派生类对象的信息?

谢谢

推荐答案

据我看到它有两大好处:

As far as I see it there are two major benefits:

  • 在人们写code使用该事件将识别模式,并立即知道如何使用事件
  • 本次活动的签名制作的方式,这是对变革稳健

第一点应该是pretty的明显,不需要大量的阐述。

The first point should be pretty obvious, not needing a lot of elaboration.

至于第二点,有两方面的原因(在我看来)。首先,由于发件人是对象,该事件的签名可以通过多种类型的重用。其次,因为第二个参数是一个 EventArgs的 decendant,当你介绍一下你自己的 EventArgs的类这个类可以在以后扩展而不改变事件的签名。

As for the second point, there are two reasons for this (in my opinion). First, since sender is object, the event signature can be reused by several types. Second, since the second parameter is an EventArgs decendant, when you introduce your own EventArgs class this class can be extended later without altering the signature of the event.

更新
回答问题:

Update
Response to questions:

我不知道你所说的是指   能够延长EventArgs的无   改变事件?!

I’m not sure what you mean by " being able to extend EventArgs without altering the signature of the event"?!

让我们举个例子,拿下面的类:

Let's take an example, take the following class:

public class SomeClass
{
    public event EventHandler<FileEventArgs> SomethingHappened;
}
public class FileEventArgs : EventArgs
{
    public FileEventArgs(string filename)
    {
        Filename = filename;
    }
    public string Filename { get; private set; }
}

然后我们有一个消费者侦听到 SomethingHappened 事件:

static void SomethingHappenedHandler(object sender, FileEventArgs e)
{
    // do something intelligent
}

现在,让我们说,我们要扩大我们传递的事件发生了什么事该文件信息的信息:

Now, lets say that we want to extend the information that we transfer in the event with information about what happened to the file:

public enum WhatHappened
{
    Copy,
    Rename,
    Delete
}
public class FileEventArgs : EventArgs
{
    public FileEventArgs(string filename, WhatHappened whatHappened)
    {
        Filename = filename;
        WhatHappened = whatHappened;
    }
    public string Filename { get; private set; }
    public WhatHappened WhatHappened { get; private set; }
}

现在,如果我们选择的文件名发送作为事件本身的参数,我们就需要通过添加另一个参数,有效地突破所有code侦听到事件改变事件的签名。但由于我们在code以上都只是简单地增加了一个属性的 FileEventArgs 类,签名保持不变,并没有监听器需要更新(除非他们想以使用新添加的属性,这是)。

Now, if we had chosen to send the filename as a parameter in the event itself, we would need to alter the event signature by adding another parameter, effectively breaking all code that listens to the event. But since we in the code above have simply just added another property to the FileEventArgsclass, the signature remains unchanged, and no listeners need to be updated (unless they want to use the newly added property, that is).

我是写在假设,如果事件   提高内部静态方法,然后   对象源参数是没有   使用?!

Am I write in assuming that if event is raised inside static method, then "object source" parameter is of no use?!

是的,这是正确的。我通常通过静态事件(其中,说实话,是非常罕见的)发件人的说法。

Yes, that is correct. I typically pass null as the sender argument from static events (which, honestly, is very rare).

这篇关于什么是有事件符合净指引的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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