0

I have defined a Foo class and used this in Bar class, with a event handler:

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:

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?

ceremcem
  • 3,900
  • 4
  • 28
  • 66
  • 1
    Possible duplicate of [C#: event with explicity add/remove != typical event?](http://stackoverflow.com/questions/1015166/c-event-with-explicity-add-remove-typical-event) – juharr Jan 16 '16 at 11:51
  • We have solved this question but I still can not understand what they are talking over there. This question differs from that question in that this question is simpler. – ceremcem Jan 16 '16 at 14:19

1 Answers1

0
class Foo
{
    private EventHandler explicitEvent;
    public event EventHandler ExplicitEvent 
    {
       add 
       { 
           explicitEvent += value;
           FireNeededMethodHere();
       } 
       remove 
       { 
           explicitEvent -= value; 
       }
    }
}
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59