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

  • Home
  • SEARCH
  • 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 1040353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:12:48+00:00 2026-05-16T15:12:48+00:00

Good day all I wrote the following method: private void RegisterEvent(object targetObject, string eventName,

  • 0

Good day all

I wrote the following method:

private void RegisterEvent(object targetObject, string eventName, string methodName)
{
    EventInfo eventInfo = targetObject.GetType().GetEvent(eventName);
    MethodInfo method = eventInfo.EventHandlerType.GetMethod("Invoke");
    IEnumerable<Type> types = method.GetParameters().Select(param => param.ParameterType);

    DynamicMethod dynamicMethod = new DynamicMethod(eventInfo.EventHandlerType.Name, typeof (void), types.ToArray(), typeof (QueryWindow));
    MethodInfo methodInfo = typeof (QueryWindow).GetMethod(methodName, new[] { typeof (object) });

    ILGenerator ilGenerator = dynamicMethod.GetILGenerator(256);
    ilGenerator.Emit(OpCodes.Ldarg_1);
    ilGenerator.EmitCall(OpCodes.Call, methodInfo, null);

    dynamicMethod.DefineParameter(1, ParameterAttributes.In, "sender");
    dynamicMethod.DefineParameter(2, ParameterAttributes.In, "e");

    // Get an argument exception here
    Delegate methodDelegate = dynamicMethod.CreateDelegate(eventInfo.EventHandlerType, this);
    eventInfo.AddEventHandler(targetObject, methodDelegate);
}

I get ArgumentException with the message

Error binding to target method.

in the line

Delegate methodDelegate = dynamicMethod.CreateDelegate(eventInfo.EventHandlerType, this); 

Could anyone point out on my mistake?

Thanks in advance.

  • 1 1 Answer
  • 2 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-16T15:12:49+00:00Added an answer on May 16, 2026 at 3:12 pm

    Assuming that methodName is a static method of QueryWindow, this should work:

    private static void RegisterEvent(object targetObject, string eventName, string methodName)
    {
        var eventInfo = targetObject.GetType().GetEvent(eventName);
        var method = eventInfo.EventHandlerType.GetMethod("Invoke");
        var types = method.GetParameters().Select(param => param.ParameterType);
    
        var methodInfo = typeof(QueryWindow).GetMethod(methodName, new[] { typeof(object) });
    
        // replaced typeof(void) by null      
        var dynamicMethod = new DynamicMethod(eventInfo.EventHandlerType.Name, null, types.ToArray(), typeof(QueryWindow));
    
        ILGenerator ilGenerator = dynamicMethod.GetILGenerator(256);
    
        // Using Ldarg_0 to pass the sender to methodName ; Ldarg_1 to pass the event args
        ilGenerator.Emit(OpCodes.Ldarg_0);
        ilGenerator.EmitCall(OpCodes.Call, methodInfo, null);
    
        // Added return
        ilGenerator.Emit(OpCodes.Ret); 
    
        // Removed parameter definition (implicit from DynamicMethod constructor)
    
        // Removed the target argument
        var methodDelegate = dynamicMethod.CreateDelegate(eventInfo.EventHandlerType);
        eventInfo.AddEventHandler(targetObject, methodDelegate);
    }
    

    Edit :

    Since you can use .NET 3.5, you should create an expression tree. Here is another solution:

    public class QueryWindow
    {
        public void RegisterEvent(object targetObject, string eventName, string methodName)
        {
            var eventInfo = targetObject.GetType().GetEvent(eventName);
            var sender = Expression.Parameter(typeof (object), "sender");
            var e = Expression.Parameter(typeof (EventArgs), "e");
            var body = Expression.Call(Expression.Constant(this), methodName, null, e);
            var lambda = Expression.Lambda(eventInfo.EventHandlerType, body, sender, e);
            eventInfo.AddEventHandler(targetObject, lambda.Compile() );
        }
    
        public void OnEvent(object o)
        {
            Console.WriteLine(o);
        }
    }
    

    Notice that the OnEvent method is no longer static. I also assume that events you are trying to subscribe to are events that follow .NET conventions (sender + event args). This way, we can leverage contravariance and always pass a lambda of type :

    (object sender, EventArgs e) => { /* */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day to all, I have the following problem I have to convert the
Good day all I have the following question: I would like to use Chart
Good day to all. I have a problem with the following code: textBox.addEventListener(KeyboardEvent.KEY_DOWN,handler); function
Good Day All we are trying to do is inside a trigger make sure
Good day all, first of all, what am trying to do is have an
Hi and Good day to all, Google App Script can be used in so
Good day everyone. I have been having the same problem all day at work
Good day, all. I know that this is a pretty basic question in terms
Good morning all, After a day of googling, I’m at a loss. I decided
Good day. I need to teach Windows CryptoAPI to encrypt the message with private

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.