3

Following the specifications for creating a Minimal, Complete and Verifiable code set, please see below :

using System;
using System.Data;
using System.Linq;
using System.Windows;

namespace WhatIsThis{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application {
        private void Application_Startup( object sender, StartupEventArgs e ) {
            Foo.Bar += ( S, E ) => Console.WriteLine( "Do Something!" );
        }

        protected override void OnActivated( EventArgs e ) {
            base.OnActivated( e );
            Foo.OnBar( );
        }
    }

    public static class Foo {
        private static event EventHandler _Bar;
        internal static void OnBar( ) {
            if ( _Bar != null )
                _Bar.Extension( null, EventArgs.Empty );
        }

        public static event EventHandler Bar {
            add { _Bar += value; }
            remove { _Bar -= value; }
        }

        private static void Extension(
            this EventHandler eH,
            object sender,
            EventArgs e ) {
            foreach(
                EventHandler evnt in
                    eH.GetInvocationList(
                    ).Cast<EventHandler>( ) ) {
                Console.WriteLine( evnt.Target );
                evnt( sender, e );
            }
        }
    }
}

I'm working on an extension to do some things with Event Handlers and I need to be able to discern what an event handlers target is ( basically if it is an System.ComponentModel.ISynchronizeInvoke type object ( for handling WinForms applications ) or if it is a System.Windows.Threading.DispatcherObject ( for handling WPF applications ) ).

When I look at the evnt.Target in the Extension method, I see that it is WhatIsThis.App.<>c.

I've tried casting it as several different things ( including an Action ) but it always comes through as null... and that's not helpful

What sort of object is this?

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107
  • 1
    You are attaching an anonymous event handler. The compiler creates a class member with a fictitious name and attaches it just as you would attach a declared method. – D Stanley Mar 02 '16 at 22:34
  • @DStanley Thank you. I will have to post a follow-up question... – Will Mar 02 '16 at 22:38
  • 1
    What are you doing that you care if it is a `ISynchronizeInvoke` or a `DispatcherObject`? make it the subscribers responsibility to invoke to the UI thread or capture a `SynchronizationContext` in the constructor of your class and post to it when you raise your events (this is what `BackgroundWorker` does for its `RunWorkerCompleted` event). – Scott Chamberlain Mar 02 '16 at 22:58

1 Answers1

2

What sort of object is this?

The name is generated by the compiler. I've posted some notes on the name pattern that the compiler used at the time here:

Where to learn about VS debugger 'magic names'

Note that these are subject to change at any time, and I have not updated that answer since I wrote it in 2010. As the comments note, more magic name patterns were added after that.

In your case though it is a "c", which is a closure class generated for a lambda.

Community
  • 1
  • 1
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Thanks. I'm posting a follow-up question now : http://stackoverflow.com/questions/35759749/how-can-i-extract-the-real-target-from-an-event-handler-that-was-defined-with – Will Mar 02 '16 at 22:48