Why delegate types are derived from MulticastDelegate class why not it directly derive from Delegate class?

醉酒当歌 提交于 2020-01-03 17:25:14

问题


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.


回答1:


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.

From CLR via C#, 3rd edition:

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. [...]



来源:https://stackoverflow.com/questions/4833053/why-delegate-types-are-derived-from-multicastdelegate-class-why-not-it-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!