为什么我的事件处理程序的目标和LT;> C吗?此外 - 什么甚至为<> C吗? [英] Why is my event handlers target <>c? Also - What even is <>c?

查看:192
本文介绍了为什么我的事件处理程序的目标和LT;> C吗?此外 - 什么甚至为<> C吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规格的创建最小的,完整的和可验证的代码集,请参阅以下内容:

 使用系统; 
使用System.Data这;
使用System.Linq的;使用System.Windows
;

命名空间WhatIsThis {
///<总结> $ B $为App.xaml中
///< b ///交互逻辑; /总结>
公共部分类应用:应用程序{
私人无效Application_Startup(对象发件人,StartupEventArgs E){
Foo.Bar + =(S,E)=> Console.WriteLine(做一些事情!);
}

保护覆盖无效OnActivated(EventArgs的发送){
base.OnActivated(E);
Foo.OnBar();
}
}

公共静态类Foo {
私人静态事件的EventHandler _Bar;
内部静态无效的OnBar(){
如果(_Bar!= NULL)
_Bar.Extension(NULL,EventArgs.Empty);
}

公共静态事件的EventHandler酒吧{
加{_Bar + =价值; }
拆下{_Bar - =价值; }
}

私有静态无效扩展(
此事件处理诶,
对象发件人,
EventArgs五){
的foreach($ B在
eH.GetInvocationList $ b事件处理EVNT(
).Cast<&事件处理GT;()){
Console.WriteLine(evnt.Target);
EVNT(发件人,E);
}
}
}
}



我M工作的扩展,做一些事情与事件处理程序,我需要能够辨别的事件处理程序的目标是什么(基本上,如果它是一个System.ComponentModel.ISynchronizeInvoke类型的对象(用于处理的WinForms应用程序),或者如果它是一个System.Windows.Threading.DispatcherObject(用于处理WPF应用程序))。



当我看 evnt.Target 扩展的方法,我看到它是 WhatIsThis.App<> C。



我试过铸造这几种不同的东西(包括动作),但它总是通过为空......这不是有益



什么样的对象是什么?


解决方案

什么样的对象为b
$ b

由编译器生成的名字呢?


$。我已经张贴在名称模式的一些注意事项,在这里时使用的编译器:



在哪里了解与调试魔法姓名



注意,这些是随时变化的,我还没有更新的答案,因为我写它在2010年作为评论指出,在这之后,加入更多的魔法名称模式。



在你的情况,虽然这是一个C,这是一个lambda生成闭合的类。


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?

解决方案

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.

这篇关于为什么我的事件处理程序的目标和LT;&GT; C吗?此外 - 什么甚至为&lt;&GT; C吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆