I'm curious if is possible to get event name in event handler? I'm pretty sure, that it's possible, can you help me find solution? Ex:
public class Boy
{
public event EventHandler Birth;
public event EventHandler Die;
}
public class Mother
{
public Mother()
{
Boy son = new Boy();
son.Birth += son_Handler;
son.Die += son_Handler;
}
void son_Handler(object sender, EventArgs e)
{
// how to get event name (Birth/Die)?
throw new NotImplementedException();
}
}
I'm curious if is possible to get event name in event handler?
No, it's not. The event handler is just a method. It could be invoked directly, not in the context of any event.
What you could do is have:
public Mother()
{
Boy son = new Boy();
son.Birth += (sender, e) => Handler("Birth", sender, e);
son.Die += (sender, e) => Handler("Die", sender, e);
}
void Handler(string description, object sender, EventArgs e)
{
Log("I'm in the {0} event", description);
throw new NotImplementedException();
}
See more on this question at Stackoverflow