Get the App
SLTechnology News&Howtos  ›  Development  › 

How to treat events as objects in .NET

Shulou Source: shulou.com Published: 2022-06-02 03:38:34 09月19日 Update

Most people do not understand the knowledge of this article "how to treat an event as an object in .NET", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to treat events as objects in .NET" article.

Taking the event as an object sometimes elevates the level of the event, which can effectively improve the abstract ability of the language. Sometimes there are mistakes, but it's worth trying.

If a language (platform) treats events as objects, it treats events as first-class citizens of the language. This means that we can pass a single event to the method as a parameter or as part of an object, which effectively improves the abstract ability of the language. Just imagine, without a "delegate", it would be impossible to treat a "method" as an object in .NET, and it would be difficult to use today's flexible abstractions. Similarly, because .NET itself cannot handle events as a single object, it will be tied up at some point, and it is difficult to introduce some special programming model.

This is the practical meaning of "passing events as objects".

We propose a "solution" that allows us to write code like this:

Class Program {public event EventHandler Submit; static void Main (string [] args) {Program p = new Program (); var ev = EventFactory.Create (() = > p.Submit); ev + = (sender, eventArgs) = > Console.WriteLine (sender); p.Submit ("Hello World", EventArgs.Empty); Console.WriteLine ("Press any key to exit..."); Console.ReadLine ();}}

It looks so like it, and the way it is used doesn't seem to be much different from traditional events. But at the end of the article, I mentioned that here is actually a bit of "fooling", and our assembly head students, and "the assembly head is as good as the assembly head" RednaxelaFX, these two pure men also pointed out the problem.

If you believe in your head, you will live forever. Believe in RednaxelaFX, resurrect in full state.

The "hoax" of the above code is that the code that operates Program.Submit is within the Program class. We can't do this if we want to manipulate events of other classes in the same way, for example:

Public class MyClass {public event EventHandler MyEvent;} class Program {static void Main (string [] args) {var myClass = new MyClass (); var ev = EventFactory.Create (() = > myClass.MyEvent);}}

There seems to be no problem with such code, but the compiler prompts an error like this:

The event 'SimpleConsole.MyClass.MyEvent' can only appear on the left hand side of + = or-= (except when used from within the type' SimpleConsole.MyClass')

The compiler tells us that except inside the MyClass class, the MyEvent event can only appear to the left of the + = or-= operation. The two pure men mentioned earlier have also had related and derivative discussions in the previous comments. Therefore, what we are doing now is a failure.

Some friends in the previous comments also mentioned that we can actually pass an event as a parameter to a method (and then add or remove handlers in the method), as long as we use the ref keyword, for example:

Static void RegisterHandlers (ref EventHandler e) {...}

Then:

Static void Main (string [] args) {var myClass = new MyClass (); RegisterHandlers (ref myClass.MyEvent);}

Obviously, however, this approach will encounter the same problem: unless it is an event within Program, we cannot pass it like a delegate object. Moreover, even if it can be passed, we can only add or remove handlers for it, not as part of another object, and then after various processing, we can also operate on this event.

So, what we're trying to implement is a type like this:

Public class DelegateEvent {... Public DelegateEvent AddHandler (TDelegate handler) {...} public DelegateEvent RemoveHandler (TDelegate handler) {.}} the above is about "how to treat events as objects in .NET". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Event object content code method head language processing practice that is time problem assembly valid same parameter meaning article way man Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS NVidia Linux OPPO Reno