[英]How to write a custom event handler adder method in C#
I have defined a Foo
class and used this in Bar
class, with a event handler: 我定义了一个Foo
类,并在Bar
类中将其与事件处理程序一起使用:
public class Bar
{
public Bar()
{
Foo foo = new Foo();
foo.my_custom_event += my_event_handler;
}
public void my_event_handler()
{
// do work here
}
}
and this works perfectly. 这很完美。 But I need to define a method in Foo
class that will be fired when I add an event handler to my_custom_event
, like: 但是我需要在Foo
类中定义一个方法,当我向my_custom_event
添加事件处理程序时将触发该方法,例如:
public class Foo
{
...
public/private void my_event_handler_adder(target_function)
{
functions_that_are_fired_on_my_custom_event.Append(target_function);
}
}
Is there any way to define such an adder method? 有什么方法可以定义这种加法器方法吗?
class Foo
{
private EventHandler explicitEvent;
public event EventHandler ExplicitEvent
{
add
{
explicitEvent += value;
FireNeededMethodHere();
}
remove
{
explicitEvent -= value;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.