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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:02:03+00:00 2026-06-10T20:02:03+00:00

I want to hook up a generic delegate at runtime, you can check the

  • 0

I want to hook up a generic delegate at runtime, you can check the sample code below.

However on binding delegate to event with Delegate.CreateInstance I am getting an ArgumentException (‘Error binding to target method.’).

class MyClass
{
    public event EventHandler<MyEventArgs> OnRequest;
}

class MyEventArgs : EventArgs { }

class Program
{
    static void Main(string[] args)
    {
        MyClass obj = new MyClass();
        EventInfo eventInfo = obj.GetType().GetEvent("OnRequest");
        Type delegateType = eventInfo.EventHandlerType;
        MethodInfo methodInfo = typeof(Program).GetMethod("OnRequestReceived");
        Delegate del = Delegate.CreateDelegate(delegateType, methodInfo);
    }

    public static void OnRequestReceived(object o, EventArgs e) { }
}
  • 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-06-10T20:02:05+00:00Added an answer on June 10, 2026 at 8:02 pm

    When you create a delegate for a static method you need to pass null instead of an object instance and if you were creating for an instance method it would have to be a reference to an instance of the object that defines the handler method and not an instance of the object that defines the event.

    Delegate.CreateDelegate(delegateType, null, methodInfo);
    

    In this scenario you could simplify even further and just use the overload that required only the delegate type and method info.

    Delegate.CreateDelegate(delegateType, methodInfo);
    

    Also note, that in the sample code you provided you’re just creating the delegate and not actually attaching it to the event. In order to bind it to the event you also need to the following:

    eventInfo.AddEventHandler(obj, del);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can use install-data-hook to do anything I want after my data
I want to hook a mouse wheel event to my scroll bar but i
I want to have a pre-commit hook check if the current commit is a
Using the Infragistics UltraGrid (e.g. myGrid ), I want to: Hook an event that
I want a global hook to the mouse and have an event fire when
hi I want to hook another application so when it closes I can close
I want to hook another program's window, so I can get when the cursor
I want to create a 'generic' JPanel which can be rendered in any of
I want to hook paste event for <input type=text> and force this text to
I have a class of div MyClass I want to hook up to two

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.