为什么我的事件处理程序的目标是 <>c?另外 - <>c 到底是什么?

2024-02-04

规格 https://stackoverflow.com/help/mcve要创建最小、完整和可验证的代码集,请参见以下内容:

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 );
            }
        }
    }
}

我正在开发一个扩展来使用事件处理程序执行一些操作,并且我需要能够辨别事件处理程序目标是什么(基本上它是否是 System.ComponentModel.ISynchronizeInvoke 类型对象(用于处理 WinForms 应用程序)或者如果它是是一个System.Windows.Threading.DispatcherObject(用于处理WPF应用程序)。

当我看着evnt.Target in the Extension方法,我看到它是WhatIsThis.App.<>c.

我尝试将其转换为几个不同的东西(包括 Action ),但它总是显示为 null...这没有帮助

这是什么样的物体?


这是什么样的物体?

该名称由编译器生成。我在这里发布了有关编译器当时使用的名称模式的一些注释:

在哪里可以了解 VS 调试器“神奇名称” https://stackoverflow.com/questions/2508828/where-to-learn-about-vs-debugger-magic-names/2509524#2509524

请注意,这些可能会随时更改,而且自从 2010 年写下该答案以来,我还没有更新过该答案。正如评论所指出的,此后添加了更多神奇的名称模式。

在你的例子中,它是一个“c”,它是为 lambda 生成的闭包类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的事件处理程序的目标是 <>c?另外 - <>c 到底是什么? 的相关文章

随机推荐