c# - 为什么委托(delegate)类型派生自 MulticastDelegate 类为什么不直接派生自 Delegate 类?

标签 c# .net delegates multicastdelegate

我有一个关于委托(delegate)类型的非常基本的问题。我在对象浏览器中比较了 DelegateMulticastDelegate 类的成员,但在 MulticastDelegate 中找不到任何新成员。

我还注意到 Delegate 类有 GetInvocationList 虚方法。所以我假设 Delegate 类应该能够保存对多个方法的引用。如果我的假设是正确的,我想知道为什么自定义委托(delegate)类型不直接派生自 Delegate 类而不是 MulticastDelegate 类。

不确定我在这里遗漏了什么。请帮助我了解其中的区别。

最佳答案

DelegateMulticastDelegate 的拆分基本上是出于历史原因。最初会有不能合并的委托(delegate)和可以合并的委托(delegate)……但事实证明这不是一个有用的区别。显然,只有在将 MulticastDelegate 从框架/CLR 中剥离出来为时已晚时才被发现。

通过 C# 从 CLR,第 3 版:

The System.MulticastDelegate class is derived from System.Delegate, which is itself derived from System.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 have MulticastDelegate as a base class, you'll occasionally manipulate your delegate types by using methods defined by the Delegate class instead of the MulticastDelegate class. [...]

关于c# - 为什么委托(delegate)类型派生自 MulticastDelegate 类为什么不直接派生自 Delegate 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833053/

相关文章:

c# - 在 C# 中创建永久自动完成文本框?

ios - 委托(delegate)方法或全局声明来为 UIButton 添加操作?

c# - 为什么我得到不正确的 Levenshtein 距离?

c# - 在 C# 中使用 Sentry 记录未处理的异常

c# - 我可以在 C# 库中有全局预处理器定义吗?

c# - 检索在 Func 中执行的调用方法的名称

iphone - 调用按钮操作时 EXC_BAD_ACCESS(代码 = 2 或代码 1)

c# - 如何等待 BackgroundWorker 完成然后退出控制台应用程序

c# - C# 中的 foreach 循环是否保证评估顺序?

.net - 这是在 vb.net 中用 null 初始化字符串的最佳方法