代表们在哪里?构造函数和成员函数是如何定义的?

发布于 2024-09-25 19:16:50 字数 260 浏览 1 评论 0原文

当我在 Reflector 中查看 Action 委托时,我看到它有一个类似的构造函数

public Action(object @object, IntPtr method);

,但我找不到相同的任何主体以及其他成员函数,例如 InvokeBeginInvoke 等。我只能看到它的定义。这些函数是在哪里定义的?它们是在 .net BCL 之外定义的吗?

When I was looking at the Action delegates in Reflector, I saw it has a constructor like

public Action(object @object, IntPtr method);

But I could not find any body for the same along with other member functions like Invoke, BeginInvoke etc. I can only see the definitions for it. Where does these functions are defined? Are they defined outside of the .net BCLs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-10-02 19:16:50

基本上,委托是由 CLR 专门处理的。编译器提供签名,但 CLR 知道如何处理它们。

ECMA-335 分区的第 8.9.3 节我谈到了这一点:

委托是面向对象的函数指针的等价物。与函数指针不同,委托是
面向对象、类型安全且可靠。委托是通过定义派生自基类型的类来创建的
System.Delegate(请参阅分区 IV)。每个委托类型应提供一个名为 Invoke 的方法,并带有适当的
参数,并且委托的每个实例都会将对其 Invoke 方法的调用转发给一个或多个兼容的
特定对象的静态或实例方法。它委托的对象和方法是在以下情况下选择的:
委托实例已创建。

除了实例构造函数和 Invoke 方法之外,委托还可以选择另外两个
方法:BeginInvoke 和 EndInvoke。这些用于异步调用。

虽然在大多数情况下,委托似乎只是另一种用户定义的类,但它们紧密相连
受控。这些方法的实现由 VES 提供,而不是用户代码。唯一额外的
可以在委托类型上定义的成员是静态方法或实例方法。

(VES 是虚拟执行系统;CLR 是 Microsoft 对 VES 的实现。)

Delegates are handled specially by the CLR, basically. The compiler provides the signatures, but the CLR knows what to do with them.

Section 8.9.3 of ECMA-335 partition I talks about this:

Delegates are the object-oriented equivalent of function pointers. Unlike function pointers, delegates are
object-oriented, type-safe, and secure. Delegates are created by defining a class that derives from the base type
System.Delegate (see Partition IV). Each delegate type shall provide a method named Invoke with appropriate
parameters, and each instance of a delegate forwards calls to its Invoke method to one or more compatible
static or instance methods on particular objects. The objects and methods to which it delegates are chosen when
the delegate instance is created.

In addition to an instance constructor and an Invoke method, delegates can optionally have two additional
methods: BeginInvoke and EndInvoke. These are used for asynchronous calls.

While, for the most part, delegates appear to be simply another kind of user-defined class, they are tightly
controlled. The implementations of the methods are provided by the VES, not user code. The only additional
members that can be defined on delegate types are static or instance methods.

(VES is the Virtual Execution System; the CLR is Microsoft's implementation of the VES.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文