In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.