我有一个关于委托(delegate)类型的非常基本的问题。我在对象浏览器中比较了 Delegate
和 MulticastDelegate
类的成员,但在 MulticastDelegate
中找不到任何新成员。
我还注意到 Delegate
类有 GetInvocationList
虚方法。所以我假设 Delegate
类应该能够保存对多个方法的引用。如果我的假设是正确的,我想知道为什么自定义委托(delegate)类型不直接派生自 Delegate
类而不是 MulticastDelegate
类。
不确定我在这里遗漏了什么。请帮助我了解其中的区别。
最佳答案
Delegate
和MulticastDelegate
的拆分基本上是出于历史原因。最初会有不能合并的委托(delegate)和可以合并的委托(delegate)……但事实证明这不是一个有用的区别。显然,只有在将 MulticastDelegate
从框架/CLR 中剥离出来为时已晚时才被发现。
通过 C# 从 CLR,第 3 版:
The
System.MulticastDelegate
class is derived fromSystem.Delegate
, which is itself derived fromSystem.Object
. The reason why there are two delegate classes is historical and unfortunate; there should be just one delegate class in the FCL.
Sadly, you need to be aware of both of these classes because even though all delegate types you create haveMulticastDelegate
as a base class, you'll occasionally manipulate your delegate types by using methods defined by theDelegate
class instead of theMulticastDelegate
class. [...]
关于c# - 为什么委托(delegate)类型派生自 MulticastDelegate 类为什么不直接派生自 Delegate 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833053/