Why is my event handlers target <>c? Also - What even is <>c?

折月煮酒 提交于 2019-12-07 15:34:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35759481/why-is-my-event-handlers-target-c-also-what-even-is-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!