莫非CLR支持"函数指针"值类型?指针、函数、类型、CLR

2023-09-03 06:07:36 作者:冷忆ゆ Conquer

前几天我问为什么代表们引用类型的,根据我的所有你需要一个委托错误的概念是两个引用:一个物体,一个一个的功能。我完全忽略(不是因为我不知道,只是因为我忘了)的是,在.NET中,代表们至少的部分的到位,以支持事件作为内置的实施< A HREF =htt​​p://en.wikipedia.org/wiki/Observer_pattern> Observer模式,这意味着每个代表通过的调用列表的方式支持多个用户。

A few days ago I asked why delegates are reference types, based on my misguided notion that all you need for a delegate are two references: one to an object, and one to a function. What I completely overlooked (not because I wasn't aware, simply because I forgot) is that in .NET, delegates are at least partially in place to support events as a built-in implementation of the Observer pattern, which means that every delegate supports multiple subscribers by way of an invocation list.

这让我思考,代表真正发挥两个不同的角色,在.NET世界中。一个是一个不起眼的函数指针,如:

This got me thinking, delegates really play two different roles in the .NET world. One is that of a humble function pointer, such as:

Action<string> writeLine = Console.WriteLine;

在等的是,可观察到的:

The other is that of an observable:

textBox.TextChanged += HandleTextChanged;

这是调用列表的存在似乎是专为第二个角色,如喜欢简单的的WriteLine 上面的例子中,你一般不会连的觉得情况的有关用户。

The existence of an invocation list seems to be exclusively for the second role, as in cases like the simple writeLine example above you generally don't even think about subscribers.

因此​​,其实,在我看来,还有的可以的是两个不同种代表了:函数指针那种和可观察的一种。前者,在我看来,可能是值类型。

So really, it seems to me there could be two different "kinds" of delegates: the "function pointer" kind, and the "observable" kind. The former, it seems to me, could be a value type.

现在,我不认为这种的应的是这种情况,如果它甚至有可能。我肯定会有很多不足之处,以进行定期和多路广播委托,之间的区别,如拳击可能高频,如果代表们的值类型,可能需要引入一个新的关键字(组播?),不可避免的开发混乱,等我真正想知道的只是,如果它的将会的是可能的,从CLR的角度来看,有一个值类型这可以作为一个函数指针。

Now, I'm not arguing that this should be the case, if it's even possible. I am sure there would be a lot of downsides to making this distinction between regular and multicast delegates, such as the likely high frequency of boxing if delegates were value types, the possible need to introduce a new keyword (multicast?), the inevitable developer confusion, etc. What I'm really curious to know is simply if it would be possible, from a CLR perspective, to have a value type that could act as a function pointer.

我想询问这将是另一种方法:就是 System.Delegate ,其调用列表和所有的,基本上是一个根本的CLR类型;或者是围绕一个简单的函数参考类型,根本不被任何CLR语言接触的包装?

I guess another way of asking this would be: is System.Delegate, with its invocation list and all, basically a fundamental CLR type; or is it a wrapper around a simpler "function reference" type that simply isn't exposed by any CLR languages?

我为人人的非正式术语我使用的可能混淆了一些更多的教育开发商那里道歉。

I apologize for all of the informal terms I've used that may have confused some of the more educated developers out there.

推荐答案

在CLR的非常早期的日子里曾经是(函数指针等)和System.MulticastDelegate(事件等)System.Delegate之间的区别。那是在.NET 1.0发布报废,有没有方法来创建一个派生自委托委托类型的实例。这实在是没有必要的。 MulticastDelegate进行了优化,不仅打造调用列表,当有多个用户。 System.Delegate是preserved出于某种原因,可能是太多的工作将其删除。

In the very early days of the CLR there used to be a distinction between System.Delegate (function pointer like) and System.MulticastDelegate (event like). That was scrapped before .NET 1.0 shipped, there is no way to create an instance of a delegate type that derives from Delegate. It just wasn't necessary. MulticastDelegate was optimized to only create its invocation list when there's more than one subscriber. System.Delegate was preserved for some reason, probably too much work to remove it.