问题描述
我有关于委托类型一个非常基本的问题。我比较代表和MulticastDelegate类的对象浏览器中的承包商,客人,我无法找到任何新的附加成员present在MulticastDelegate。我也注意到,委托类有GetInvocationList虚方法。所以,我认为Delegate类应保持引用多个方法的能力。如果我的假设是正确的我不知道为什么没有自定义的委托类型直接从代理类而不是MulticastDelegate类派生。不知道我在这里缺少什么。请帮我理解上的差异。
I have a very basic question regarding delegate types. I compared the memebers of Delegate and MulticastDelegate classes in object browser and I couldn't find any new additional member present in MulticastDelegate. I also noticed that the Delegate class has GetInvocationList virtual method. So I assume that the Delegate class should have the capability to hold references to multiple methods. If my assumption is correct I wonder why not custom delegate types directly derive from the Delegate class instead of MulticastDelegate class. Not sure what I am missing here. Please help me understand the difference.
推荐答案
代表的基本分割
和 MulticastDelegate
是历史原因。本来有打算是代表其的无法的结合以及那些可能......但竟然不是一个有用的区别。显然,这是只有在发现这是一个有点来不及撕 MulticastDelegate
出框架/ CLR的。
Basically the split of Delegate
and MulticastDelegate
is for historical reasons. Originally there were going to be delegates which couldn't be combined and ones which could... but that turned out not to be a useful distinction. Apparently that was only discovered when it was a bit too late to rip MulticastDelegate
out of the framework/CLR.
这是通过C#CLR,第3版:
From CLR via C#, 3rd edition:
的System.MulticastDelegate类是从System.Delegate衍生,其本身从System.Object而得。之所以有两个委托类是历史的和不幸的;应在FCL只是一个委托类。可悲的是,你需要知道这两个类的,因为即使你创建所有委托类型有MulticastDelegate作为基类,你会偶尔通过使用Delegate类,而不是MulticastDelegate类中定义的方法操纵你的委托类型。 [...]
这篇关于为什么要委托类型是从MulticastDelegate类派生为什么不直接从代理类派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!