gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:49:56 24 4
gpt4 key购买 nike

specifications 之后要创建最小的、完整的和可验证的代码集,请参见下文:

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.TargetExtension方法,我看到它是 WhatIsThis.App.<>c .

我试过将它转换为几个不同的东西(包括一个 Action ),但它总是以 null 出现...这没有帮助

这是什么东西?

最佳答案

What sort of object is this?

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

Where to learn about VS debugger 'magic names'

请注意,这些内容随时可能更改,自从我在 2010 年撰写该答案以来,我还没有更新过它。正如评论所述,之后添加了更多神奇的名称模式。

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

关于c# - 为什么我的事件处理程序是目标 <>c?另外 - <>c 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com