Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the compilation of events and delegates in C #

Shulou Source: shulou.com Published: 2022-06-02 09:05:48 09月16日 Update

This article is about how to implement the compilation of events and delegates in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Compilation requirements for C# events and delegates

When you manipulate a C # event, you sometimes get a compilation error: the event "Delegate.GreetingManager.MakeGreet" can only appear to the left of + = or-= (except when used from the type "Delegate.GreetingManager").

At this point, we comment out the line of the compilation error, recompile it, and then use Reflactor to explore the declaration statement of event to see why such an error occurred:

Public event GreetingDelegate MakeGreet

As you can see, in fact, although we declare MakeGreet as public in GreetingManager, MakeGreet is actually compiled into private fields. No wonder the above compilation error occurred, because it simply does not allow access by assignment outside the GreetingManager class, thus verifying the inference we made above.

Compiled code for C# events and delegates

Let's take a closer look at the code generated by MakeGreet:

Private GreetingDelegate MakeGreet; / / A declaration of a pair of events is actually a declaration of a private delegate variable [MethodImpl (MethodImplOptions.Synchronized)] public void add_MakeGreet (GreetingDelegate value) {this.MakeGreet = (GreetingDelegate) Delegate.Combine (this.MakeGreet, value);} [MethodImpl (MethodImplOptions.Synchronized)] public void remove_MakeGreet (GreetingDelegate value) {this.MakeGreet = (GreetingDelegate) Delegate.Remove (this.MakeGreet, value);}

It is now clear that the MakeGreet event is indeed a delegate of type GreetingDelegate, but whether it is declared as public or not, it is always declared as private. In addition, it has two methods, add_MakeGreet and remove_MakeGreet, for registering delegate types and unregistering them, respectively. In fact, "+ =" corresponds to add_MakeGreet, and "- =" corresponds to remove_MakeGreet. The access restrictions for these two methods depend on the access qualifier when the event is declared.

Inside the add_MakeGreet () method, the Combine () static method of System.Delegate is actually called, which is used to add the current variable to the delegate list. We mentioned twice before that a delegate is actually a class, when we define a delegate:

Public delegate void GreetingDelegate (string name)

When the compiler encounters this code, it generates the following complete class:

Public sealed class GreetingDelegate:System.MulticastDelegate {public GreetingDelegate (object @ object, IntPtr method); public virtual IAsyncResult BeginInvoke (string name, AsyncCallback callback, object @ object); public virtual void EndInvoke (IAsyncResult result); public virtual void Invoke (string name);} Thank you for reading! This is the end of this article on "how to realize the compilation of events and delegates in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!

Tags: Delegate compile event method actual clocked actually error two code type content variable time more article private restricted nice practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Docker Linux Redmi