Where does delegates' constructors and member functions are defined?

前端 未结 1 1974
耶瑟儿~
耶瑟儿~ 2020-12-21 13:21

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

public Action(object @object, IntPtr method);


        
相关标签:
1条回答
  • 2020-12-21 13:41

    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.)

    0 讨论(0)
提交回复
热议问题