我如何提取“真实”商品?从匿名定义的事件处理程序获取目标? [英] How can I extract the "real" target from an Event Handler that was defined anonymously?

查看:102
本文介绍了我如何提取“真实”商品?从匿名定义的事件处理程序获取目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注我的问题此处

根据发表的评论-


编译器会创建一个具有虚拟名称的类成员,并将其附加,就像附加声明的方法一样。

The compiler creates a class member with a fictitious name and attaches it just as you would attach a declared method.

我不完全理解这意味着,但我可以验证是否可以代替

I do not fully comprehend what this means but I can verify that if instead of saying

Foo.Bar += (S, E) => { /*Code Goes Here*/ }

我说

Foo.Bar += FooBar;

private void FooBar( object sender, EventArgs e ){
    /*Code Goes Here*/
}

然后 Event.Target WhatIsThis.App。<> c WhatIsThis.App

这太棒了,但我不能保证我会永远写一个附加到事件处理程序的实际方法。

That's awesome but I can't guarantee that I will always write an actual method to attach to an event handler.

在我确实使用匿名方法的情况下,有没有办法提取 real

In the cases where I do use an anonymous method, is there a way to extract the real target, or am I just pigeonholed into using defined methods (I mean, I can live with that I guess but if there's some high-tech sorcery that I can employ to extract the real target, then I'm all for it ).

推荐答案


不能完全理解这是什么意思

do not fully comprehend what this means

让我们解决您的理解问题。假设您有:

Let's fix your comprehension. Suppose you have:

class C 
{
  int x;
  void M(int y) 
  { 
    int z = GetZ(); 
    Func<int, int> f = q => q + x + y + z;
    ... 

f是一个委托。它具有接收器和作为接收器方法的方法。 (请注意,这实际上不是严格的要求,但对于我们今天的用途而言,这种极端情况并不明显。)

f is a delegate. It has a receiver and a method that is a method of the receiver. (Note, this is not actually a strict requirement but the corner case is obscure for our purposes today.)

具有该方法的接收器是什么类型

接收者等于 this 的是C吗?不,为什么不呢?因为那然后我们如何跟踪y和z的值? M的每次调用可能会有不同的值,因此接收者不能为 this

Can it be C, with the receiver equal to this? No. Why not? Because then how do we keep track of the value of y and z? There could be a different value for every invocation of M, so the receiver cannot be this.

编译器是什么确实会产生一个新的类:

What the compiler does is generates a new class:

class C 
{
  int x;
  class Locals
  {
    public C __this;
    public int y;
    public int z;
    public int A(int q) { return q + __this.x + y + z; }
  }

  void M(int y) 
  {
    Locals locals = new Locals();
    locals.__this = this;
    locals.y = y;
    locals.z = GetZ(); 
    Func<int, int> f = locals.A;
    ... 

那么接收者是什么?当地人的价值。用什么方法? A。

So what is the receiver? the value of locals. What is the method? A.

当然,本地人和A都被赋予了疯狂的名字,因此您不会意外地将它们命名。

Of course both Locals and A are given crazy names so that you cannot call them by accident.


在我确实使用匿名方法的情况下,有没有一种方法可以提取实际目标

In the cases where I do use an anonymous method, is there a way to extract the real target

我保证,您正在提取真正的接收器。

You are extracting the real receiver, I promise.


正在使用扩展程序来对事件处理程序进行某些操作,我需要能够辨别目标事件处理程序是什么

'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

请不要那样做。事件处理程序的接收者是提供处理程序的代码的实现细节。您无法做出决定。编译器完全有权利在为事件处理程序生成接收器时做出他们喜欢的任何选择,并且他们愿意。考虑一下,如果事件处理程序是在迭代器块或异步方法中创建的,会发生什么情况。或者该事件被某些反应式扩展代码预订,这些代码将序列操作应用于该事件。同样,编译器将在各处生成类。您不能依靠该类是明智的。

Please don't do that. The receiver of an event handler is an implementation detail of the code that provided the handler. It's not there for you to make decisions on. Compilers are well within their rights to make any choice they like when generating a receiver for an event handler, and they do. Consider what happens if the event handler was created inside an iterator block, or an async method, for example. Or the event is being subscribed by some reactive extensions code that is applying sequence operations to the event. Again, the compiler will be generating classes all over the place. You can't rely on the class being something "sensible".

您可以依靠的是:订阅者希望在发生某些事情时调用给定的方法。那是您必须遵守的合同;不要试图猜测订户。

The thing you can rely on is: the subscriber wished the given method to be called when something happened. That's the contract you must obey; don't try to second-guess the subscriber.

这篇关于我如何提取“真实”商品?从匿名定义的事件处理程序获取目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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