首页 新闻 赞助 找找看

c#高级中有这么一句话“对同一个委托调用方法链的顺序并未正式定义应避免编写依赖于以特定顺序调用方法的代码”,我想知道为什么?

0
悬赏园豆:20 [已解决问题] 解决于 2011-03-29 10:28

c#高级中有这么一句话“对同一个委托调用方法链的顺序并未正式定义应避免编写依赖于以特定顺序调用方法的代码”,我想知道为什么?经过测试,对于多播委托方法调用顺序和添加方法的顺序是一致的,为什么那个老外要这么说?到底是什么意思?

steve.z的主页 steve.z | 初学一级 | 园豆:180
提问于:2011-03-28 20:51
< >
分享
最佳答案
0

When invoking the multicast delegate, each delegate instance in the linked list is called sequentially. (Generally, delegates are called in the order they were added but this behavior is not specified within the CLI specification and furthermore, it can be overridden. Therefore, programmers should not depend on an invocation order.) This sequential invocation, however, leads to problems if the invoked method throws an exception or if the delegate itself returns data.

http://msdn.microsoft.com/en-us/vcsharp/bb508935.aspx

我也是刚才看到你问题才知道的,

如果你有C++经验的话,这种case就见怪不怪了 :)

收获园豆:20
Jerry Chou | 老鸟四级 |园豆:2642 | 2011-03-28 21:42
我还有个问题,“ it can be overridden”在什么情况下回overridden?
steve.z | 园豆:180 (初学一级) | 2011-03-29 09:00
是指CLI没有做出承诺,所以后续版本可能overridden吗?
steve.z | 园豆:180 (初学一级) | 2011-03-29 09:03
是的,只是规范没有定义。
所以最好不要写出依赖于这于规范中未定义的特性的代码。
通过这样的代码不具有移植性(包括.net不同版本,.net mono)。
Jerry Chou | 园豆:2642 (老鸟四级) | 2011-03-29 10:15
谢谢,结贴了。
steve.z | 园豆:180 (初学一级) | 2011-03-29 10:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册