CLR可以支持“功能指针”值类型? [英] Could the CLR support a "function pointer" value type?

查看:131
本文介绍了CLR可以支持“功能指针”值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我询问了为什么代表是参考类型,基于我的错误的观念是,代理所需要的是两个引用:一个对象,一个对一个函数。我完全忽视了(不是因为我不知道,只是因为我忘了)是在.NET中,代表至少是部分地来支持事件作为内置的< a href =http://en.wikipedia.org/wiki/Observer_pattern =noreferrer>观察者模式,这意味着每个代表都可以通过调用列表支持多个订阅者

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;

其他是可观察的:

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.

现在,我并不是说这个应该是这种情况,如果有可能的话我相信,在常规和多播代理之间存在很大的缺点,例如,如果代表是价值类型,可能会有很高的拳击频率,可能需要引入一个新的关键字( multicast ?),不可避免的开发者混乱等等。我真正好奇的是,只要它能够从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.Delegate(函数指针)和System.MulticastDelegate(类似事件)之间的区别。在.NET 1.0发布之前已经被废止了,没有办法创建从Delegate派生的委托类型的实例。它没有必要。 MulticastDelegate已被优化,以便在有多个订阅者时仅创建其调用列表。 System.Delegate由于某些原因被保留,可能太多的工作要删除它。

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.

这篇关于CLR可以支持“功能指针”值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆