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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:32:54+00:00 2026-05-15T23:32:54+00:00

In .Net 4 this code run with no error class A { public event

  • 0

In .Net 4 this code run with no error

class A
{
   public event EventHandler<CustomEventArgs> MyEvent;
}

where CustomEventArgs derived from EventArgs

void Test()
{
  A a = new A();
  a.MyEvent += MyFunc;
}
void MyFunc(object sender, EventArgs args) // EventArgs expect of CustomEventArgs
{
}

Now, when I try to do the same by reflection I get can’t cast delegate exception

EvemtInfo myEvent = ... // get event somehow
myEvent.AddEventHandler(a, new EventHandler<EventArgs>(ConnectResponseReceived));

How can I do the same it in reflection?

  • 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-15T23:32:55+00:00Added an answer on May 15, 2026 at 11:32 pm

    You can use Delegate.CreateDelegate to help here:

    using System;
    using System.Reflection;
    
    namespace ConsoleApplication13
    {
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A(); // the object *raising* the event
                EventInfo evt = a.GetType().GetEvent("MyEvent");
                B b = new B(); // the object *handling* the event (or null for static)
                MethodInfo handler = b.GetType().GetMethod("ConnectResponseReceived");
                Delegate del = Delegate.CreateDelegate(evt.EventHandlerType, b, handler);
                evt.AddEventHandler(a, del);
                a.OnMyEvent(); // test it
            }    
        }
        class A
        {
            public event EventHandler<CustomEventArgs> MyEvent;
            public void OnMyEvent() {
                var handler = MyEvent;
                if(handler != null) handler(this, new CustomEventArgs());
            }
        }
        class B
        {
            public void ConnectResponseReceived(object sender, EventArgs args) 
            {
                Console.WriteLine("got it");
            }
        }
        class CustomEventArgs : EventArgs { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static class Extensions{ public static void Dump<T>(this T o) { } public static
I have this code : http://jsfiddle.net/VAkLn/6/ When i add a table : http://jsfiddle.net/VAkLn/7/ this
This code : http://jsfiddle.net/bYtTG/1/ Works as I want in FF, chrome and opera, but
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I use this code to load a .Net assembly to PowerShell: [System.Reflection.Assembly]::Load(System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
I have this code in my ASP.NET application written in C# that is trying
I just found this code in reflector in the .NET base libraries... if (this._PasswordStrengthRegularExpression
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {
I have this code to download a file, but on sourceforge.net sever there is
Could anyone please help me convert this code to vb.net, I have tried it

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.