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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:46:26+00:00 2026-05-17T21:46:26+00:00

Is it possible to use Reflection is C# to fire an event? Say I

  • 0

Is it possible to use Reflection is C# to fire an event? Say I have a situation like this:

public delegate void SomeEventHandler(object sender, BenArgs e);

class EventHub
{
    public event SomeEventHandler SOME_EVENT;

    public void fireEvent(String eventName)
    {
        SomeEventHandler evt = (SomeEventHandler) Delegate.CreateDelegate(typeof(SomeEventHandler), this, eventName);

        evt.Invoke(null, null);
    }
}

Meaning if I call

EventHub.fireEvent("SOME_EVENT")

then it makes the event hub fire SOME_EVENT? I’ve tried this and am just getting exceptions.

This is mostly a curiosity of mine, I know that in this case I could just as easily fire the event without 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-17T21:46:26+00:00Added an answer on May 17, 2026 at 9:46 pm

    Assuming your current scenario, i.e.:

    1. A field-like event.
    2. The backing delegate-field has the same name as the event.
    3. (this, EventArgs.Empty) are valid arguments to pass to the delegate.

    You can do something like this (more argument validation required):

    public void fireEvent(String eventName)
    {
        // Get a reference to the backing field
        var del = (Delegate)typeof(EventHub)
                    .GetField(eventName, BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(this);
    
         // Invoke the delegate, it's invocation-list will contain the listeners
         if(del != null)
            del.DynamicInvoke(this, EventArgs.Empty);
    }
    

    Usage:

      var eHub = new EventHub();
      eHub.SOME_EVENT += delegate { Console.WriteLine("SOME_EVENT fired.."); };      
      eHub.fireEvent("SOME_EVENT");
    

    Now, you can generalize this idea with an extension method on object if you like, but all of this is a really bad idea. This problem can’t be solved in the general case because one can’t know how an event is “implemented.” There could be arbitrary code inside the add and remove methods, and the logic to “fire” the event could also be just about anything. There might not even be a backing multicast delgate field to store the listeners.

    In any case, trying to tell an object to fire an event is almost always a sign of a major design-flaw, IMO. If you still really do want this, it would be much better to declare a method like this in your class:

    public void RaiseXXXEvent() { ... } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if it's possible to use reflection to locate an object at runtime?
I have recently started looking into Google Charts API for possible use within the
Is it possible to use reflection to know what the code is doing, not
Is it possible to use Reflection.Emit to create types in an existing assembly, or
Is it possible to use reflection when we write a code-template? I was just
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Is it possible to use a flash document embedded in HTML as a link?
Is it possible to use Apache Subversion (SVN) as general purpose backup tool? (As
Is it possible to use Microsoft Entity Framework with Oracle database?
Is it possible to use an UnhandledException Handler in a Windows Service? Normally I

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.