在构造函数中注册事件? [英] Registering events inside a constructor?

查看:21
本文介绍了在构造函数中注册事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩委托、事件和匿名方法.这样做有一点变得非常清楚.

I have been playing around with delegates, events, and anonymous methods. In doing so one point became very clear.

它不会简化在构造函数中注册任何事件方法或委托函数的过程吗?

Would it not streamline the process of registering any event methods or delegate functions in the constructor?

我的测试表明它是有效的,它可以防止你在实例化后声明它们(因为对象的构造函数为你做了).

My tests shows it works and it prevents you from having to declare them after instantiation (as the constructor of the object does it for you).

其实性能还是不错的.在构造/实例化对象时使用this"关键字来引用当前对象有什么缺点吗?

In fact, the performance is pretty good. Are there any drawbacks to using the "this" keyword to refer to the current object when constructing / instantiating the object?

这对我来说似乎很有意义,因为所有事件都会在实例化时连接起来.

This seems to make a lot of sense to me as all the events would be wired up on instantiation.

是否有任何领域可能存在问题?

Are there any areas this might be an issue?

例子:

//Constructor
public SayHello() 
{
  _name = "Unnamed";
  _isUpdated = false;

  // Register event handler via lambda (ananymous method shorthand)
  this.NameChanged += (object sender, EventArgs e) => { Console.WriteLine(e.message)); };
}

推荐答案

这种方法存在一些潜在问题.首先,在更一般的方面,出于性能原因,人们通常应该倾向于使用方法覆盖而不是订阅自我发布的事件.显然,如果事件是由基于外部来源的类公开的,该类公开了没有相应可覆盖方法的事件,则这是不可能的.但是,订阅自行发布的活动应该是万不得已的方法,而不是默认方法.

There are a couple of potential problems with this approach. First, on the more general side, one should usually favour using method overrides over subscribing to self-published events for performance reasons. Obviously, this isn't possible if the event is exposed by an externally sourced based class that exposes an event without a corresponding overridable method. However, subscribing to self-published events should be an approach of last resort, not a default approach.

第二个潜在问题更严重,但它与事件触发的代码做了什么有关,而不是哪个对象暴露了事件.例如,考虑以下构造函数:

The second potential problem is more serious, but it has to do with what the code triggered by the event does, not which object exposes the event. For example, consider the following constructor:

public Foo(Bar bar)
{
    bar.SomeEvent += (s, e) => this.DoSomething();
}

如果 bar 在另一个线程上触发 SomeEvent,您的 Foo 实例的 DoSomething 方法可能会在实例完全初始化之前被调用.这是 Java 领域中一个有据可查的问题(例如,参见 http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html),但 C#/.NET 的覆盖范围要少得多.http://joeduffyblog.com/2010/06/27/on-partiallyconstructed-objects/ 提供了一些 .NET 的详细介绍,但可能比您想知道的要多...

If bar fires SomeEvent on another thread, your Foo instance's DoSomething method could be called before the instance has been fully initialized. This is a fairly well-documented problem in the Java space (see, for example, http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html), but coverage is much sparser for C#/.NET. http://joeduffyblog.com/2010/06/27/on-partiallyconstructed-objects/ provides some detailed coverage for .NET, but it might be more than you wanted to know...

这篇关于在构造函数中注册事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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