gpt4 book ai didi

c# - 委托(delegate)的构造函数和成员函数在哪里定义?

转载 作者:行者123 更新时间:2023-11-30 21:19:18 26 4
gpt4 key购买 nike

当我查看 Reflector 中的 Action 委托(delegate)时,我看到它有一个像这样的构造函数

public Action(object @object, IntPtr method);

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

最佳答案

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

ECMA-335 的第 8.9.3 节分区我讲这个:

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 是虚拟执行系统;CLR 是 Microsoft 对 VES 的实现。)

关于c# - 委托(delegate)的构造函数和成员函数在哪里定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3862152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com