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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:24:42+00:00 2026-06-15T21:24:42+00:00

I want to subscribe to an EventAggregator event using Reflection, because I am trying

  • 0

I want to subscribe to an EventAggregator event using Reflection, because I am trying to dynamically wire up event subscriptions between Prism modules at runtime. (I’m using Silverlight 5, Prism and MEF).

What I want to achieve is calling _eventAggregator.GetEvent<MyType>().Subscribe(MyAction) in one of my modules, but I am stuck at calling _eventAggregator.GetEvent<MyType>(). How can I proceed from there to call Subscribe(MyAction)?

Say my Event class is public class TestEvent : CompositePresentationEvent<string> { }. I don’t know this at compile time, but I know the Type at runtime.

Here is what I have got so far:

 Type myType = assembly.GetType(typeName); //get the type from string

 MethodInfo method = typeof(IEventAggregator).GetMethod("GetEvent");
 MethodInfo generic = method.MakeGenericMethod(myType);//get the EventAggregator.GetEvent<myType>() method

 generic.Invoke(_eventAggregator, null);//invoke _eventAggregator.GetEvent<myType>();

I would really appreciate a pointer in the right direction.

  • 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-15T21:24:43+00:00Added an answer on June 15, 2026 at 9:24 pm

    You can do this without having to worry about the “type” of event you are invoking by using dynamics.

    Type eventType = assembly.GetType(typeName);
    MethodInfo method = typeof(IEventAggregator).GetMethod("GetEvent");
    MethodInfo generic = method.MakeGenericMethod(eventType);
    dynamic subscribeEvent = generic.Invoke(this.eventAggregator, null);
    
    if(subscribeEvent != null)
    {
        subscribeEvent.Subscribe(new Action<object>(GenericEventHandler));
    }
    
    //.... Somewhere else in the class
    
    private void GenericEventHandler(object t)
    {
    }
    

    Now you really never need to know what the “event type” is.

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

Sidebar

Related Questions

I want to subscribe and unsubscribe from a ToolStripButton 's Click event using a
I'm creating a number of UserControl s dynamically using LoadControl(String) and want to subscribe
I have a ValueChanged event. I want to subscribe to it using Rx only
i'm currently experimenting wpf communication between two views of different modules using prism IEventAggregator.
In my class I want to declare an event that other classes can subscribe
Consider the situation in which you want to subscribe to an event for one
I want to subscribe to the AppDomain.CurrentDomain.UnhandledException event to catch any errors as im
I have a function that I want to subscribe it to a event queue.
I often run into a situation where I want to subscribe to an event,
I want that people trying to subscribe on my website to enter a nickname

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.