组播代理C#中的奇怪行为? [英] Multicast delegate weird behavior in C#?

查看:90
本文介绍了组播代理C#中的奇怪行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的事件:

public class ClassA
{
    public event Func<string, int> Ev;
    public int Do(string l)
    {
        return Ev(l);
    }
}

和2方法:

  static int Display(string k)
        {
            return k.Length;
        }

  static int Display_2(string k)
        {
            return k.Length*10;
        }

我注册此活动:

 ClassA a = new ClassA();
 a.Ev += Display;
 a.Ev += Display_2;

现在,我正在执行:

   Console.WriteLine(a.Do("aaa")); 

输出:

什么??? / strong>

What ???


  • 他已经在调用列表 2 方法!它运行它们,但是为什么它仅显示最后一次注册的结果?

  • he has in invocation list 2 methods ! it did run them , but why does it shows only the result from the last registration ?

3的结果已经消失了? (第一次调用)? (虽然显示 + display_2 已执行...我没想到 console.write 来迭代结果,但也没有指望他决定要显示哪个。

Where does the result of "3" has gone ? ( the first invocation ) ? ( although both display+display_2 was executed... I didn't expect console.write to iterate through results . but also didn't expect him to decide which to show.)

推荐答案

在这里玩三个方面:


  1. 事件的实现

  2. 代理组合的行为

  3. 调用一个代表其调用列表有多个条目

对于第1点,您有一个类似的字段事件。 C#4规范的10.8.1节给出了一个例子,并说:

For point 1, you have a field-like event. Section 10.8.1 of the C# 4 spec gives an example, and states that:


按钮类,单击成员只能在 + = - = 运算符,如

Outside the declaration of the Button class, the Click member can be used only on the left-hand saide of the += and -= operators, as in

b.Click += new EventHandler(...);

其中将代理追加到<$ c $的调用列表 c>点击事件

which appends a delegate to the invocation list of the Click event

(强调我的)。该规范还清楚地表明,类似场景的事件创建一个委托字段,该委派字段从 中用于调用类。

(emphasis mine). The spec also makes it clear that a field-like event creates a delegate field, which is used from within the class for invocation.

更多一般(第2点),C#4规范第7.8.4节通过 + + =

More generally (point 2), section 7.8.4 of the C# 4 spec talks about delegate combination via + and +=:


代表组合。每个委托类型隐含地提供以下预定义的运算符,其中 D 是委托类型:

D operator +(D x, D y)

二进制 + operato执行委托组合,当两个操作数都有一些委托类型 D 。 [...跳过位,其中 x y 为null ...]否则,操作结果为一个新的委托,当被调用时,调用第一个操作数,然后调用第二个操作数

The binary + operato performs delegate combination when both operands are of some delegate type D. [... skip bits where x or y are null ...] Otherwise, the result of the operation is a new delegate that, when invoked, invokes the first operand and then invokes the second operand.

再次,强调我的。)

最后,点3 - 事件调用和返回值。 C#规范的第15.4节说明:

Finally, point 3 - event invocation and return values. Section 15.4 of the C# spec states:


如果委托调用包含输出参数或返回值,则其最终值将来自调用的最后一位代表。

If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

更一般来说,这取决于事件的实现。如果您使用使用正常代理组合/删除步骤的事件实现,则一切都将得到保证。如果你开始写一个疯狂的自定义实现,那就是另一回事。

More generally, it depends on the event implementation. If you use an event implementation which uses the "normal" delegate combination/removal steps, everything is guaranteed. If you start writing a custom implementation which does crazy things, that's a different matter.

这篇关于组播代理C#中的奇怪行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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