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 7662397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:47:30+00:00 2026-05-31T13:47:30+00:00

Consider: someControl.Click += delegate { Foo(); }; The arguments of the event are irrelevant,

  • 0

Consider:

someControl.Click += delegate { Foo(); };

The arguments of the event are irrelevant, I don’t need them and I’m not interested in them. I just want Foo() to get called. There’s no obvious way to do the same via reflection.

I’d like to translate the above into something along the lines of

void Foo() { /* launch missiles etc */ }

void Bar(object obj, EventInfo info)
{
    Action callFoo = Foo;
    info.AddEventHandler(obj, callFoo);
}

Also, I don’t want to make the assumption that the type of object passed to Bar strictly adheres to the guidelines of using the EventHander(TArgs) signature for events. To put it simply, I’m looking for a way to subscribe an Action to any handler type; less simply, a way to convert the Action delegate into a delegate of the expected handler type.

  • 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-31T13:47:32+00:00Added an answer on May 31, 2026 at 1:47 pm
    static void AddEventHandler(EventInfo eventInfo, object item,  Action action)
    {
      var parameters = eventInfo.EventHandlerType
        .GetMethod("Invoke")
        .GetParameters()
        .Select(parameter => Expression.Parameter(parameter.ParameterType))
        .ToArray();
    
      var handler = Expression.Lambda(
          eventInfo.EventHandlerType, 
          Expression.Call(Expression.Constant(action), "Invoke", Type.EmptyTypes), 
          parameters
        )
        .Compile();
    
      eventInfo.AddEventHandler(item, handler);
    }
    static void AddEventHandler(EventInfo eventInfo, object item, Action<object, EventArgs> action)
    {
      var parameters = eventInfo.EventHandlerType
        .GetMethod("Invoke")
        .GetParameters()
        .Select(parameter => Expression.Parameter(parameter.ParameterType))
        .ToArray();
    
      var invoke = action.GetType().GetMethod("Invoke");
    
      var handler = Expression.Lambda(
          eventInfo.EventHandlerType,
          Expression.Call(Expression.Constant(action), invoke, parameters[0], parameters[1]),
          parameters
        )
        .Compile();
    
      eventInfo.AddEventHandler(item, handler);
    }
    

    Usage:

      Action action = () => BM_21_Grad.LaunchMissle();
    
      foreach (var eventInfo in form.GetType().GetEvents())
      {
        AddEventHandler(eventInfo, form, action);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider this function: jQuery.fn.MyFunction = function (event) { alert(this.text()); } I want to call
Consider the need to develop a lightweight desktop DB application on the Microsoft platforms.
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider the following 2 queries: select tblA.a,tblA.b,tblA.c,tblA.d from tblA where tblA.a not in (select
Consider such html piece: <p>foo</p><p>bar</p> If you run (for example) jQuery text for it
Consider the following string: LoReM {FOO} IPSUM dolor {BAR} Samet {fooBar} I'm looking for
Consider the following code: Class Demo { Person person = new Person(foo); // assume
Consider i have a dataGrid that is created dynamically. Now i need to add
Consider the following code (you can just put this in the developer console in
Consider this snippet of Jade: if(#{episode[question.name][field]}) #{episode[question.name][field]} I want to check if the value

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.