多播委托如何执行? [英] how does multicast delegate execute?

查看:112
本文介绍了多播委托如何执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能告诉多播委托如何执行?


我对代表的工作感到非常困惑.


请通过提供任何实时示例或代码来帮助我.



谢谢
Seema

Hi,

Could anyone tell how does multicast delegate execute?


I am very confused in delegates working.


Please help me in this by giving any live example or code.



Thanks
Seema

推荐答案

请参考:
C#中的多播代表 [ MulticastDelegate类 [多播代表 [深度学习C#代理和事件 [多播代表执行顺序 [
Please refer:
MULTICAST DELEGATES IN C#[^]
MSDN: MulticastDelegate Class[^]
Multicast Delegates[^]
C# Delegates And Events in Depth[^]

..and more threads about multicast delegate execution order[^]


此处解释起来不是很简单,并且Microsoft文档在大多数情况下都非常清晰和全面,使代表的大多数方面保持沉默.不过,请阅读所有文档,然后再阅读我的答案,因为这件事很难理解.

首先要了解的是:作为类型的委托和与委托实例(类/结构的成员)不同的是,它们与类和实例的联系方式不同.委托实例的运行时类型与委托类型(!)无关.使用反射,您可以检查任何实例,并查看其类型是否是某些类.而且委托类型不是.

第二个令人困惑的事情是多播.实际上,发生的事情是:您声明了一个委托实例,并且它始终为null.令人惊讶的是,您可以使用"+ ="运算符在处理程序的调用列表中添加处理程序.这是实际初始化委托实例的时刻(另一种方法是将一个实例分配给另一个实例,以获取引用同一委托实例的引用,但是这种方式您不会创建任何实例;没有其他方式).该委托似乎是单例的.您可以再添加一个处理程序.它将始终有效.没注意!您现在将获得多播委托实例(在其调用列表中包含两个元素).但是这始终是一个全新的实例,与以前的实例不同.当您将它们进行参考比较时,可以轻松地对其进行检查.当您再添加一个处理程序时,您将再次获得一个多播实例(当然是3个处理程序),但这又是一个全新的实例! 委托实例是不可变的.为什么?答案是:多线程.

关于代表还有其他一些奇妙的事实.

我在文章中介绍了更多详细信息:
动态方法分派器 [
欢迎您提出后续问题.

—SA
This is not very simple to explain, and the Microsoft documentation, quite clear and comprehensive on most cases, keeps silence about most aspects of delegates. Nevertheless, please read all this documentation before reading my answer, because this matter is pretty hard to understand.

First thing to understand is: delegates as types and delegate instances (members of a class/structure) are not related in the same way as classes and instances. The run-time type of the delegate instance have nothing to do with delegate type (!). With Reflection, you can examine any instance and see that its type is some class. And a delegate type is not.

Second confusing thing is the multicast. Actually, what happen is: you declare a delegate instance, and it''s always null. Amazingly, you can add a handler the its invocation list using "+=" operator. This is the moment when a delegate instance is actually initialized (another way is to assign one instance to another to get to references referencing the same delegate instance, but this way you don''t create any instances; there are no other ways). This delegate appears to be single-case. You can add one more handler to it. It will always work. Not attention! You will get multcast delegate instance now (with two elements in its invocation list). But this is always a brand new instance not the same as the previous instance. You can easily check it when you compare them referentially. When you add one more handler, you get a multicast instance again (of course, 3 handler), but this is a brand new instance again! The delegate instances are immutable. Why? The answer is: multithreading.

There are some other wonderful facts about the delegates.

I described some more detail in my article: Dynamic Method Dispatcher[^], please see the section "4.1. On the Nature of Delegate Instance".

Your follow-up questions are welcome.

—SA


这篇关于多播委托如何执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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