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

标签 c# .net delegates reflector

当我在 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/

相关文章:

c# - 无法在 Win XP 机器上运行 netsh 命令

.net - 您如何为 .NET 项目实现持续集成?

c# - C# 中的 Action 委托(delegate)

c++ - 在函数指针之间转换

c# - 在 C# 中从服务器向客户端发送一些约束

c# - 如何制作 Response.Write(...);在我的 Controller 中

c# - Excel 添加编程

c# - 轻型 HTML 部分匹配优化

c# - 如何在 ListView 子项中显示图标

ios - 为什么MKMapView的mapView是:viewForOverlay: not called when userlocation is enabled?