开发者

how do I subscribe to an event in raised in another assembly

开发者 https://www.devze.com 2022-12-16 23:28 出处:网络
I have a solution which contains 3 project.One project handles asynchronous communinications.When it has completed it\'s callback, it raises an event SomethingCompleted.

I have a solution which contains 3 project. One project handles asynchronous communinications. When it has completed it's callback, it raises an event SomethingCompleted. How do I subscribe to this e开发者_如何学Pythonvent from another project in the same solution?

I have the event handlers built in the receiving project but it does not see the event in the sending project.


There's nothing special about events raised in a separate assembly, unless the event itself is declared with the internal access modifier. Check that it's public.

To give an example of what I mean, I'm sure you don't think twice about subscribing to Button.Click - but Button is in a different assembly, isn't it?


The event must be declared as public in the class which defines it:

public class Something
{
  public event EventHandler SomethingCompleted;
}

You can then subscribe to it just as to any other event:

Something s = ...;
s.SomethingCompleted += SomeEventHandler;


You need to have a reference to the class raising the event to register an event handler.

Once you have that, it's done like registering any other event handler:

foo.SomethingCompleted += (sender, e) => this.DoSomething();
0

精彩评论

暂无评论...
验证码 换一张
取 消