Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:06:28+00:00 2026-05-17T00:06:28+00:00

RELATED Watch control to determine events being fired? I need to detect when an

  • 0

RELATED

  • Watch control to determine events being fired?

I need to detect when an event is fired. To do so I am trying to subscribe dynamically on the event.

The problem is that I have different types of delegates, not all events have the same signature. The solutions provided in here and here expects object sender, EventArgs e, which I am not using, so I get an exception telling the types doesn’t match.

Here are some examples of a delegates I have:

public delegate void OnEventA(int id);
public delegate void OnEventB(double num, string name);

How can I create the correct delegate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:06:29+00:00Added an answer on May 17, 2026 at 12:06 am

    After some research I found some articles:

    • How to: Hook Up a Delegate Using Reflection
    • Delegate.CreateDelegate Method (Type, Object, MethodInfo, Boolean)

    It helped me to understand what I was trying to do and I should do.

    I need to use Delegate.CreateDelegate passing the EventHandlerType (the type of the event, the delegate), a instance of a class and the method info of the method (from the class in the previous parameter) that will handle the event. Target is the control that fires this event.

    Delegate handler = Delegate.CreateDelegate(evt.EventHandlerType, abc, mi1, false);
    evt.AddEventHandler(target, handler);
    

    Further digging lead me to this method. I can subscribe to events using lambda expression. Using Action<T> I can subscribe with different types and numbers of parameters.

    public static Delegate Create<T>(EventInfo e, Action<T> a)
    {
        var parameters = e.EventHandlerType.GetMethod("Invoke").GetParameters().Select(p => Expression.Parameter(p.ParameterType, "p")).ToArray();
        var exp = Expression.Call(Expression.Constant(a), a.GetType().GetMethod("Invoke"), parameters);
        var l = Expression.Lambda(exp, parameters);
        return Delegate.CreateDelegate(e.EventHandlerType, l.Compile(), "Invoke", false);
    }
    

    Using this method (e is the EventInfo; EventManager is the class with the static method above)

    e.AddEventHandler(this, EventManager.Create<int>(e, (x) => Console.WriteLine("Execute")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem trying to work out how to watch a folder for
I am facing problem While using watch in gdb . I am trying to
Related to an earlier question I asked, I've seen that there's both LoadComponent() and
Related question [stackoverflow] here . I'm trying to do the above, but I want
Related to a question I asked earlier here , I've found a problem which
Related to this question , is the idea of a default servlet that serves
I have a problem related to Memory Leak. In my app I have to
Im looking for a tool that will watch directory(with sub-dirs) and give me the
Related: Correct way to document open-ended argument functions in JSDoc I've a function that
The problem is the variable nodeType is being changed after STRCPY finishes running. nodeType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.