EventArgs 类和 event 关键字之间是否存在特殊关联?

发布于 2024-10-16 15:34:18 字数 1236 浏览 7 评论 0原文

在所有 .NET 书中,我阅读了实现事件的指南,其中解释了您需要子类化 EventArgs 并使用 EventHandler。我在 http://msdn.microsoft.com/en-us/library/ms229011 上查找了更多信息。 aspx,它显示“请使用 System.EventHandler,而不是手动创建新的委托来用作事件处理程序。”我知道使用 EventArgs 有重要的原因,但我的问题不是“我应该这样做吗?”,而是“我可以这样做吗?”。

是否有任何原因导致我无法在事件中使用通用委托而不是 EventHandler ?例如,如果我想要一个强类型的发送者(其他人会被那个对象发送者惹恼吗?)。

为了更好地解释我的意思,以下是否有任何原因不起作用?

public class IoC
{
    public AbstractFactory GetAbstractFactory()
    {
        var factory = new AbstractFactory();
        factory.CreateObject += ()=>new object();
        return factory;
    }
}
public class AbstractFactory
{
    public event Func<object> CreateObject;

    private object OnObjectCreated()
    {
        if(CreateObject == null)
        {
            throw new Exception("Not injected.");
        }
        return CreateObject();
    }


    private object _injectedObject;
    public object InjectedObject
    {
        get
        {
            if(_injectedObject == null)
            {
                _injectedObject = OnObjectCreated();
            }
            return _injectedObject;
        }
    }
}

In all the .NET book I've read the guide line for implementing events explains that you need to subclass EventArgs and use EventHandler. I looked up more info on http://msdn.microsoft.com/en-us/library/ms229011.aspx, and it says "Do use System.EventHandler instead of manually creating new delegates to be used as event handlers." I understand that there are important reasons to use EventArgs, but my question is not "Should I do it this way?", but "Can I do it this way?".

Is there any reason that I can't use a generic delegate instead of an EventHandler with my events? For example, if I want a strongly-typed sender (anyone else get annoyed by that object sender?) .

To explain what I mean better, is there any reason the following won't work?

public class IoC
{
    public AbstractFactory GetAbstractFactory()
    {
        var factory = new AbstractFactory();
        factory.CreateObject += ()=>new object();
        return factory;
    }
}
public class AbstractFactory
{
    public event Func<object> CreateObject;

    private object OnObjectCreated()
    {
        if(CreateObject == null)
        {
            throw new Exception("Not injected.");
        }
        return CreateObject();
    }


    private object _injectedObject;
    public object InjectedObject
    {
        get
        {
            if(_injectedObject == null)
            {
                _injectedObject = OnObjectCreated();
            }
            return _injectedObject;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-10-23 15:34:18

这只是约定,对语言没有要求。您可以使用任何委托类型作为事件。

标准 EventHandler 签名有一些优点:

  1. 您可以扩展 EventArgs 参数。如果您想要传递给事件处理程序的每一项都只有一个参数,那么这将不起作用。
  2. 接受 EventArgs 基类的 EventHandler 可以订阅遵循约定的任何事件。
  3. 您可以向出现在所有事件上的 EventHandler 添加扩展方法。
  4. 返回类型为void。其他返回类型作为事件处理程序没有多大意义。
  5. 你遵守惯例。遵循惯例通常是一个好主意,除非你有令人信服的理由不这样做。

It's just convention, and no requirement of the language. You can use any delegate type as an event.

The standard EventHandler<T> signature has a few advantages though:

  1. You can extend the EventArgs parameter. This wouldn't work if you had one parameter for each thing you want to pass into the eventhandler.
  2. An EventHandler which accepts the EventArgs base-class can subscribe to any event following the convention
  3. You can add Extension methods to EventHandler<T> which appear on all events.
  4. Return type is void. Other return types don't make much sense as eventhandlers.
  5. You're following the convention. Following the convention is usually a good idea, unless you have compelling arguments not to.
栀子花开つ 2024-10-23 15:34:18

Microsoft 的所有文档都是关于基类库的设计和/或通用框架设计指南。您可以使用任何您想要的图案。

也就是说,如果人们将使用您的代码,那么如果您遵循 Microsoft 使用的模式,他们会更加熟悉。

All of the documentation from Microsoft is about the design of the base class libraries and/or general framework design guidelines. You can use whatever pattern you want.

That said, if people will be consuming your code it will be more familiar to them if you follow the patterns that Microsoft uses.

凝望流年 2024-10-23 15:34:18

据我所知,EventHandler 和 EventArgs 是最佳实践,但没有什么可以阻止您在事件声明中使用任意委托。 event 关键字为您提供了能够 += 和 -= 委托给事件槽的特殊功能,而不是简单地拥有委托类型的字段或属性,后者仅接受单个委托(除非您组成多个委托)你自己)。

警告:我不确定事件槽中具有返回值的委托会发生什么情况。我的猜测是返回值被丢弃,因为将返回值分配给事件的多个委托将很难处理。但这需要一些实验。

As far as I know, EventHandler and EventArgs are best practices, but there is nothing to to stop you from using any arbitrary delegate in an event declaration. The event keyword gives you the special functionality of being able to += and -= delegates to the event slot, as opposed to simply having a field or property of the delegate type, which will only accept a single delegate (unless you compose multiple delegates yourself).

Caveat: I'm unsure what happens to delegates with return values in an event slot. My guess is return values are discarded, since multiple delegates with return values assigned to the event would be hard to handle. This would require some experimentation though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文