How to make event handler delegate know which event is calling it

My code boiled down to the actual problem: (out of a larger project for unittests)

// define all needed delegates for all events in myInstance
EventHandler oea = (o, ea) => GenericEventHandler(o, ea);
EventHandler<int> oi = (o, i) => GenericEventHandler(o, i);
// more 

myInstance.GetType().GetEvents().Foreach(ei => {
  // here I check the delegate of EventInfo to se which handler to use
  ....
  AssignEvent(myItem, ei, oea); // or
  AssignEvent(myItem, ei, oi);  // or even more
  ....
});
...
private static void AssignEvent(object instance, EventInfo @event, Delegate handler)
{
    var iName = instance.GetType().Name;
    WriteTrace($"Assign {@event.EventHandlerType.Name} to {iName}.{@event.Name}");
    @event.AddEventHandler(instance, handler);
}
private static void GenericEventHandler(params object[] o)
{
    WriteTrace("$Event from {....} invoked");
}
```

I would like to know the eventname calling the handler at runtime. The @event.Name is right there, but how do I let the handler know about it.

Since the eventhandler is hooked up this way, the StackTrace from within the GenericEventHandler does not contain any clues to the name of the event.

I have looked at DynamicMethod, Reflection, Expressions and much more, but could not find a way to do it.

I would like to stay away from Emit and IL.

4

If you change the signature of GenericEventHandler to first accept ÈventInfo instance you can do this:

void Main() {
    var item = new A();
    var eventInfo = item.GetType().GetEvents().FirstOrDefault();
    // just to have the definition before your loop
    Func<EventInfo, EventHandler<int>> eventHandlerGenerator = (eventInfo)
        => new EventHandler<int>((o, ea) => GenericEventHandler(eventInfo, o, ea));
    
    // inside your loop
    EventHandler<int> oi = eventHandlerGenerator(eventInfo);
    AssignEvent(item, eventInfo, oi);
    item.InvokeEvent();
    
}

private static void GenericEventHandler(EventInfo eventInfo, params object[] o) {
    Console.WriteLine($"Event from {eventInfo.Name} invoked");
}


private static void AssignEvent(object instance, EventInfo @event, Delegate handler) {
    var iName = instance.GetType().Name;
    @event.AddEventHandler(instance, handler);
}

class A {
    public event EventHandler<int> SomeEvent;

    public void InvokeEvent() {
        SomeEvent.Invoke(this, 42);
    }
}

You can do this with Expressions

void Main()
{
    var item = new Item();
    foreach(var eventInfo in item.GetType().GetEvents())
    {
        AssignEvent(item, eventInfo, GenericEventHandler);
    }
    Console.WriteLine("==================");
    item.RaiseClick(1);
    item.RaiseClick(2);
    item.RaiseClick2(1);
    item.RaiseClick2(2);
    item.RaiseDoubleClick("db click1");
    item.RaiseDoubleClick("db click2");
    item.RaiseOther(null);
    item.RaiseOther(new EventArgs());
    item.RaiseCustom(1, DateTime.Now, "test", null);
}

static Delegate CreateDelegate(EventInfo eventInfo, CommonEventHandler handler)
{
    var eventType = eventInfo.EventHandlerType;
    var eventHandlerParameters = eventType.GetMethod("Invoke").GetParameters();
    var parametersList = new List<ParameterExpression>();
    foreach (var parameterInfo in eventHandlerParameters)
    {
        parametersList.Add(Expression.Parameter(parameterInfo.ParameterType));
    }
    var array = Expression.NewArrayInit(typeof(object), parametersList.Select(pe => pe.Type.IsValueType ? Expression.Convert(pe, typeof(object)) : (Expression)pe));
    var handlerCall = Expression.Call(handler.Target == null ? null : Expression.Constant(handler.Target), handler.Method, Expression.Constant(eventInfo), array);
    var lambda = Expression.Lambda(eventType, handlerCall, false, parametersList);
    lambda.ToString().Dump();
    return lambda.Compile();
}


static void AssignEvent(object instance, EventInfo eventInfo, CommonEventHandler handler)
{
    var iName = instance.GetType().Name;
    Console.WriteLine($"Assign {eventInfo.EventHandlerType.Name} to {iName}.{eventInfo.Name}");
    eventInfo.AddEventHandler(instance, CreateDelegate(eventInfo, handler));
}

delegate void CommonEventHandler(EventInfo eventInfo, params object[] o);

static void GenericEventHandler(EventInfo eventInfo, params object[] o)
{
    Console.WriteLine($"Event `{eventInfo.Name}` from '{o[0]}' invoked with parameters: {{{string.Join(", ", o.Skip(1).Select(v=>v is null ? "(null)" : v))}}}");
}

class Item
{
    public event EventHandler<int> Click;
    public event EventHandler<int> Click2;
    public event EventHandler<string> DoubleClick;
    public event EventHandler Other;
    public event CustomDelegate Custom;
    
    public delegate void CustomDelegate(Item sender, int a, DateTime b, string c, object d);
    
    internal void RaiseClick(int i)
    {
        Click?.Invoke(this, i);
    }

    internal void RaiseClick2(int i)
    {
        Click2?.Invoke(this, i);
    }

    internal void RaiseDoubleClick(string s)
    {
        DoubleClick?.Invoke(this, s);
    }

    internal void RaiseOther(EventArgs args)
    {
        Other?.Invoke(this, args);
    }

    internal void RaiseCustom(int a, DateTime b, string c, object d)
    {
        Custom?.Invoke(this, a, b, c, d);
    }
}

You can see results here

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How to make event handler delegate know which event is calling it

My code boiled down to the actual problem: (out of a larger project for unittests)

// define all needed delegates for all events in myInstance
EventHandler oea = (o, ea) => GenericEventHandler(o, ea);
EventHandler<int> oi = (o, i) => GenericEventHandler(o, i);
// more 

myInstance.GetType().GetEvents().Foreach(ei => {
  // here I check the delegate of EventInfo to se which handler to use
  ....
  AssignEvent(myItem, ei, oea); // or
  AssignEvent(myItem, ei, oi);  // or even more
  ....
});
...
private static void AssignEvent(object instance, EventInfo @event, Delegate handler)
{
    var iName = instance.GetType().Name;
    WriteTrace($"Assign {@event.EventHandlerType.Name} to {iName}.{@event.Name}");
    @event.AddEventHandler(instance, handler);
}
private static void GenericEventHandler(params object[] o)
{
    WriteTrace("$Event from {....} invoked");
}
```

I would like to know the eventname calling the handler at runtime. The @event.Name is right there, but how do I let the handler know about it.

Since the eventhandler is hooked up this way, the StackTrace from within the GenericEventHandler does not contain any clues to the name of the event.

I have looked at DynamicMethod, Reflection, Expressions and much more, but could not find a way to do it.

I would like to stay away from Emit and IL.

4

If you change the signature of GenericEventHandler to first accept ÈventInfo instance you can do this:

void Main() {
    var item = new A();
    var eventInfo = item.GetType().GetEvents().FirstOrDefault();
    // just to have the definition before your loop
    Func<EventInfo, EventHandler<int>> eventHandlerGenerator = (eventInfo)
        => new EventHandler<int>((o, ea) => GenericEventHandler(eventInfo, o, ea));
    
    // inside your loop
    EventHandler<int> oi = eventHandlerGenerator(eventInfo);
    AssignEvent(item, eventInfo, oi);
    item.InvokeEvent();
    
}

private static void GenericEventHandler(EventInfo eventInfo, params object[] o) {
    Console.WriteLine($"Event from {eventInfo.Name} invoked");
}


private static void AssignEvent(object instance, EventInfo @event, Delegate handler) {
    var iName = instance.GetType().Name;
    @event.AddEventHandler(instance, handler);
}

class A {
    public event EventHandler<int> SomeEvent;

    public void InvokeEvent() {
        SomeEvent.Invoke(this, 42);
    }
}

You can do this with Expressions

void Main()
{
    var item = new Item();
    foreach(var eventInfo in item.GetType().GetEvents())
    {
        AssignEvent(item, eventInfo, GenericEventHandler);
    }
    Console.WriteLine("==================");
    item.RaiseClick(1);
    item.RaiseClick(2);
    item.RaiseClick2(1);
    item.RaiseClick2(2);
    item.RaiseDoubleClick("db click1");
    item.RaiseDoubleClick("db click2");
    item.RaiseOther(null);
    item.RaiseOther(new EventArgs());
    item.RaiseCustom(1, DateTime.Now, "test", null);
}

static Delegate CreateDelegate(EventInfo eventInfo, CommonEventHandler handler)
{
    var eventType = eventInfo.EventHandlerType;
    var eventHandlerParameters = eventType.GetMethod("Invoke").GetParameters();
    var parametersList = new List<ParameterExpression>();
    foreach (var parameterInfo in eventHandlerParameters)
    {
        parametersList.Add(Expression.Parameter(parameterInfo.ParameterType));
    }
    var array = Expression.NewArrayInit(typeof(object), parametersList.Select(pe => pe.Type.IsValueType ? Expression.Convert(pe, typeof(object)) : (Expression)pe));
    var handlerCall = Expression.Call(handler.Target == null ? null : Expression.Constant(handler.Target), handler.Method, Expression.Constant(eventInfo), array);
    var lambda = Expression.Lambda(eventType, handlerCall, false, parametersList);
    lambda.ToString().Dump();
    return lambda.Compile();
}


static void AssignEvent(object instance, EventInfo eventInfo, CommonEventHandler handler)
{
    var iName = instance.GetType().Name;
    Console.WriteLine($"Assign {eventInfo.EventHandlerType.Name} to {iName}.{eventInfo.Name}");
    eventInfo.AddEventHandler(instance, CreateDelegate(eventInfo, handler));
}

delegate void CommonEventHandler(EventInfo eventInfo, params object[] o);

static void GenericEventHandler(EventInfo eventInfo, params object[] o)
{
    Console.WriteLine($"Event `{eventInfo.Name}` from '{o[0]}' invoked with parameters: {{{string.Join(", ", o.Skip(1).Select(v=>v is null ? "(null)" : v))}}}");
}

class Item
{
    public event EventHandler<int> Click;
    public event EventHandler<int> Click2;
    public event EventHandler<string> DoubleClick;
    public event EventHandler Other;
    public event CustomDelegate Custom;
    
    public delegate void CustomDelegate(Item sender, int a, DateTime b, string c, object d);
    
    internal void RaiseClick(int i)
    {
        Click?.Invoke(this, i);
    }

    internal void RaiseClick2(int i)
    {
        Click2?.Invoke(this, i);
    }

    internal void RaiseDoubleClick(string s)
    {
        DoubleClick?.Invoke(this, s);
    }

    internal void RaiseOther(EventArgs args)
    {
        Other?.Invoke(this, args);
    }

    internal void RaiseCustom(int a, DateTime b, string c, object d)
    {
        Custom?.Invoke(this, a, b, c, d);
    }
}

You can see results here

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật