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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:05:29+00:00 2026-05-18T02:05:29+00:00

I have a very simple class : class Trace { void WriteTrace() { Console.WriteLine(Trace

  • 0

I have a very simple class :

class Trace
{
    void WriteTrace()
    {
        Console.WriteLine("Trace !");
    }
}

I want this class subscribes to an event, for example the load event of a form control. The control and event are define dynamically so I want to use reflection to do this I’m trying something like that :

In my class Trace I have this method :

public void SubscribeEvent (Control control)
{
        if (type.IsAssignableFrom(control.GetType()))
        {
            Trace test = this;
            MethodInfo method = typeof(Trace).GetMethod("WriteTrace");

            // Subscribe to the event
            EventInfo eventInfo = control.GetType().GetEvent("Load"); // We suppose control is a form 
            Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, control, method); // ERROR : Error binding to target method

        }
    }
}

There is an error on the last line : Error binding to target method. What’s wrong in my snippet ?

Thank you !

EDIT : Ok, there is no more error but when the event “Load” is raised from the Form, the method WriteTrace is not called (I have put a breakpoint but there is not reached). Why ?

Sorry for the edit, it works very fine 🙂

  • 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-18T02:05:30+00:00Added an answer on May 18, 2026 at 2:05 am

    With a few changes, I was able to execute your sample.

    Firstly, the method on Trace must have a different signature to correspond with the EventHandler type:

    public class Trace
    {
        public void WriteTrace(object sender, EventArgs e)
        {
            Console.WriteLine("Trace !");
        }
    }
    

    Next, a few changes were made to the SubscribeEvent:

    public void SubscribeEvent(Control control)
    {
        if (typeof(Control).IsAssignableFrom(control.GetType()))
        {
            Trace test = this;
            MethodInfo method = typeof(Trace).GetMethod("WriteTrace");
    
            EventInfo eventInfo = control.GetType().GetEvent("Load");
    
            // Create the delegate on the test class because that's where the
            // method is. This corresponds with `new EventHandler(test.WriteTrace)`.
            Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, test, method);
            // Assign the eventhandler. This corresponds with `control.Load += ...`.
            eventInfo.AddEventHandler(control, handler);
        }
    }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple class with only one field member (e.g. String). Is
I have very simple select like this: SELECT * FROM table WHERE column1 IN
Very simply put: I have a class that consists mostly of static public members,
i have very simple problem. I need to create model, that represent element of
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding
I have a very simple bit of script that changes the status of an
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have a very simple Java RMI Server that looks like the following: import
I have a very simple ASP.Net page that acts as a front end for

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.