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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:42:32+00:00 2026-05-20T22:42:32+00:00

I am trying to subscribe to ALL events exposed by a WPF GridView-like 3rd

  • 0

I am trying to subscribe to ALL events exposed by a WPF GridView-like 3rd party component in order to do some debugging. Aside from the suggestion that this might not be the best way to go about debugging it and stuff like that I would like to know if this can be done.

For the routed events it worked ok like this :

var type = tree.GetType();
do
{
    var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
    foreach (var staticField in staticFields)
    {
        if (typeof(RoutedEvent).IsAssignableFrom(staticField.FieldType))
        {
            tree.AddHandler((RoutedEvent)staticField.GetValue(null), new RoutedEventHandler(OnRoutedEvent), true);
        }
    }
} while ((type = type.BaseType) != typeof(object)/* && type.FullName.StartsWith("Telerik")*/);

public void OnRoutedEvent(object sender, System.Windows.RoutedEventArgs e)
{
    Debug.WriteLine(e.RoutedEvent.ToString());
}

However, with typical events this doesn’t seem to work :

var evts = tree.GetType().GetEvents();
foreach (var ev in evts)
{
    ev.AddEventHandler(this, new EventHandler(OnEvent));
}

public void OnEvent(object sender, EventArgs e)
{
      //..
}

because it doesn’t like either the thing that the delegate is EventHandler instead of the specialized type or because the signature of the event handler method does not contain the specialized EventArgs class type.

Can this be done somehow?

———— LATER EDIT ———
In all three cases (my attempt, ds27680’s suggestion and Thomas Levesque’s suggestion) the AddEventHandler call fails with :

        System.Reflection.TargetException occurred
            Message=Object does not match target type.
            Source=mscorlib
                StackTrace:
                   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
                   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
                   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                   at System.Reflection.EventInfo.AddEventHandler(Object target, Delegate handler)
                   at Test.MainWindow..ctor() in c:\users\me\documents\visual studio 2010\Projects\Test\Test\MainWindow.xaml.cs:line 39

I guess the fact that the event handler method’s signature does not match EXACTLY the EventArgs type is what makes it fail…

  • 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-20T22:42:32+00:00Added an answer on May 20, 2026 at 10:42 pm

    You could try:

    public class DebugHook
    {
        public static void OnEvent<EventArgsType>(object sender, EventArgsType eventArgs)
        {
    
        }
    }
    

    then:

    foreach (var ev in evts)
    {
       Type argsType = getEventArgsType(ev);
    
       MethodInfo hook = typeof(DebugHook).GetMethod("OnEvent");
       MethodInfo boundEventhandler = hook.MakeGenericMethod(new [] { argsType} );
    
       Delegate handler = Delegate.CreateDelegate(ev.EventHandlerType, boundEventhandler);
    
       ev.AddEventHandler(this, handler );
    }
    

    where getEventArgs looks like this:

     public Type getEventArgsType(EventInfo eventType)
     {
         Type t = eventType.EventHandlerType;
         MethodInfo m = t.GetMethod("Invoke");
    
         var parameters = m.GetParameters();
         return parameters[1].ParameterType;
     }
    

    Of course a lot of error checking/handling is missing…

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

Sidebar

Related Questions

I'm trying to subscribe to the Like button click. Here's the code I have:
So I'm trying to do some event handling when a user clicks the like
I have set up a twitter-like following model. Users can all subscribe to each
Trying to parse an HTML document and extract some elements (any links to text
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
Trying to get comfortable with jQuery and I have encountered some sample code that
I am trying to redirect to an URL if the Facebook Like button has
I am trying to trigger the FB subscribe event, but the alert does not
I'm trying to use SDK Facebook to detect the click on the button like
I'm trying to implement simple Event Bus I started like this: public class RegistrationData

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.