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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:20:34+00:00 2026-06-02T05:20:34+00:00

If I have a type that inherits from EventArgs (lets call it EventArgs1), and

  • 0

If I have a type that inherits from EventArgs (lets call it EventArgs1), and a further bunch of classes that inherit from EventArgs1 (lets call them collectively EventArgsX), and then a bunch of events that are are of the type EventHandler<EventArgsX>, if at runtime I am passed the EventInfo for one of these events and I want to add an event handler that expects a second argument of type EventArgs1 (e.g. MyEventHandler(object sender, EventArgs1 e)) how would I do it?

If the event was of type EventHandler<EventArgs1> then I would just do this:

eventInfo.AddEventHandler(this, new EventHandler<EventArgs1>(MyEventHandler));

But this throws an exception when the event is of type EventHandler<EventArgsX>, and since I don’t know what EventArgsX is at compile time I can’t simply new up an EventHandler<EventArgsX> If I did know which event I was adding the handler to at compile time then this would be entirely acceptable:

MyEvent += MyEventHandler

But I simply can’t work out how to do this at runtime. Any suggestions?

  • 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-02T05:20:36+00:00Added an answer on June 2, 2026 at 5:20 am

    I can’t simply new up an EventHandler<EventArgsX>

    Sure you can, although you need to do it using Delegate.CreateDelegate() and reflection. Assuming MyEventHandler is an instance method on this, you could do it like this:

    var eventInfo = …;
    
    EventHandler<EventArgs1> badHandler = MyEventHandler;
    
    var goodHandler = Delegate.CreateDelegate(
        eventInfo.EventHandlerType, this, badHandler.Method);
    
    eventInfo.AddEventHandler(this, goodHandler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view, that is of Inherits from System.Web.Mvc.ViewPage<P> . Type P has
I have an anonymous type object that I receive as a dynamic from a
I have an ASCX that inherits from a WebControl that has a 'CustomConfiguration' property
I have a partial view that inherits from ViewUserControl<Guid?> - i.e. it's model is
I have a class that inherits from DrawingVisual. It declares a DependencyProperty which, when
1)I have the PageBase class that inherits from Page Class. The all code behind
I have an object called Settings that inherits from NSMutableDictionary . When I try
I have a class that inherits from TreeNode, called ExtendedTreeNode. To add an object
I have a controller that inherits from the org.springframework.web.servlet.mvc.AbstractController class I have configurated it
I have a base class Base, and class A/B that inherits from it. public

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.