Direct understanding of .NET commission and events

zhaozj2021-02-16  49

Beginners are often confused by MSDN when understanding commission and events, in order to make beginners .NET's rapid application of .NET's entrustment and event model programming, I am mainly the key to understanding, the following code can be run directly, first Look at the code below.

Using system;

Namespace delegetetest {class delegeteclass {

Public Delegate Void Fhandler (INT A); // Key - This line can be seen as a category declaration public flyler f0; public void d (int A, int b) {INT C = A B; f0 (c);}} Class test {public void output (int mun) {system.console .writeline ("{0}", mun);} [static] static void main (string [] args) {test t = new test (); delegeteclass DC = New delegeteclass (); dc.f0 = new delegetetetetest.delegeteclass.fhandler (T.output); // instance initialization DC.D (2, 3);}}}

Explain "Key": Public Delegate Void Fhandler (INT A); it can be seen as follows:

Class Fhandler {.....}

The interior of the class is automatically completed by the compiler. It is a Sealed class that can be seen by disassembly. It is a category declaration that checks the information added to your own function, such as the return value, and parameter type.

Nowadays, those who are familiar with VC may feel public delegate void fhandler (int A); this sentence is like a macro

Now that it is a definition of a class, then you can get the namespace directly.

Using system;

Namespace delegetetest {public delegate void fhandler (INT A); // fhandler now rises to the class class delegeteclass {public fhandler F0; // Declare an instance F0 of the entrusted Fhandler;

Public Fhandler F1; // can also declare an instance F1 of a FHANDLER class; public void d (int A, int b) {INT C = A B; F0 (C);}} class test {public void Output Mun) {system.console .writeline ("{0}", mun);} [stathread] static void main (string [] args) {test t = new test (); delegeteclass dc = new delegeteclass (); DC. F0 = new delegetetetest.fhandler (t.output); // This trip is made to modify DC.D (2, 3);}}} There is more than knowing public delegate void fhandler (int A) This line code can only be placed Ability to declare the class, natural fhandler. The following functions are static methods, such as fhandler.equals (...);

So what does Fhandler declare? In fact, it is declared the type of function, and the information of the function (such as return value, parameter type). Speaking of the delegation or to talk about the instance of the delegate type. In many places in MSDN, (entrusted) This term refers to an instance of the delegate type. It has a list, and each of the lists contains a reference to the objects where functions and functions are located.

When declaring the instance F0 of the Fhandler class, F0 cannot be used, it is empty, so F0 needs to initialize DC.f0 = new delegetetest.fhandler (T.output); the initialization parameter contains two information T - object Quote, Output - Function information, if you comment out of this sentence, you have to see what information - "Examples that do not set object reference to the object". In addition, the parameters and return values ​​of the Output function need to be consistent with the class declaration of FHandler, which is checked by the compiler when compiling.

After initialization, there is a data in the instance (most only one, so that this efficiency is relatively high, that is, SINGLE CAST, this time the instance is the delegate type (note is a big-written D)).

Now let's talk about multicast, in fact, the multicast that is entrusted is to call each function in the list once), but the efficiency of the multicast is not very high, so most of the instances of the entrusted are unicast (SINGLE CAST), and the example that may be commissioned will run different mechanisms according to the number of functions within the list (here we don't have to study it). Look at the following code:

Namespace delegetetest {public delegate void fhandler (int A); class delegeteclass {public fhandler f0; public fhandler f1; public void d (int A, int b) {INT C = A B; F1 (C); // Merge pointer List multicast commission}} class test {public void output (int mun) {system.console .writeline ("Function 1 Display {0}", MUN);} public void output1 (int mun) {system.console .writeline ("Function 2 Display {0}", MUN);} [stathread] static void main (string [] args) {test t = new test (); delegeteclass DC = new delegeteclass (); dc.f0 = new deleget. Fhandler (t.output); dc.f1 = new fhandler (t.output1); System.Console .writeline ("First Trigger"); DC.D (2, 3); // First Trigger DC.f1 = (fhandler) delegate.comBBine (DC.F0, DC.F1); // Merge Generate New] Function List DC.f1 = (Fhandler) MulticastDelegate.comBine (DC.F1, DC.F0); / / Sype on System.Console .writeline ("Second Trigger"); DC.D (2, 3); // Second Trigger}}} actually DC.F1 = (fhandler) multicastdeLegate.comBine (DC.f1 , DC.f0); and DC.f1 = (fhandler) delegate.comBBine (DC.F1, DC.f0); the effect is the same; by the above example, we can know that we can construct our own events through Delegate. Microsoft provides us with Event for your convenience. Using system; namespace delegetetetest {public delegate void fhandler (int A); class delegeteclass {public fhandler f0; public fhandler f1; public event fhandler f3; // Key - In fact, this line can also be seen as a macro

Public Void D (int A, int b) {INT C = A B; F3 (c); // Changing it}} Class test {public void output (int mun) {system.console .writeline (" 1 Show {0} ", MUN);} public void output1 (int mun) {system.console .writeline (" 2nd display {0} ", MUN);} [stathread] static void main (String [] ARGS {Test t = new test (); delegeteclass dc = new delegeteclass (); dc.f0 = new delegetetetest.f1 = new fhandler (t.output1); DC.f3 = new fhandler (t.output); // f3 is Event System.Console .writeline ("First Trigger"); DC.D (2, 3); // First Trigger System.Console .Writeline ("Second Time" Trigger "); DC.F3 = (fhandler) delegate.comBBine (DC.f0, DC.f1); // Add a list to F3 DC.D (2, 3); // Second trigger}}}}}}}}}}}}}}

I can see the public event fhandler F3 macro declared two members through ILDASM.

.field private class delegetetetest.fhandler F3 and a packaging for F3:

.event delegeteTest.fHandler f3 {.addon instance void delegeteTest.delegeteClass :: add_f3 (class delegeteTest.fHandler) .removeon instance void delegeteTest.delegeteClass :: remove_f3 (class delegeteTest.fHandler)},

Therefore, the red code above can be replaced with as follows:

Private fhandler _f3; // Since it cannot be the same as the attribute name, use _f3, is a private public evening fhandler f3 {add {this._f3 = value;} remove {this._f3 - = value;}}

The EVENT keyword is convenient for everyone's habits, and it (this is very important, almost uses the reason for using an Event keyword) during the external use of the class. " =" And "- =" are operated, in addition to this, and directly declare that public fhandler f3 is not different. It allows various parameters and return values ​​as the above F0, F1 (EventHandler is a specific delegate, no return value, parameters are Object and Eventargs class (or inherited)). It is based on the commission. In fact, we can use Delegate to construct our event, which is the reason why Delegate is in .NET. Let me talk about the event model of general controls.

Let me talk about the "ON event name" in the control such as: OnTextChange (Eventargs E), there are many things like, in fact this is a programming habit, he represents this TextChanged event (of course, Same, including a delegate ontextChanged and an EVENT package), specifically, a function onTextChanged (), onTextChanged () contains an ONTEXTCHANGED (E), is included in TEXTCHANGED (E). If you rewrite onTextChanged () without adding Base.OnTextChanged (e) in (rewriting function); then your handler of the TextChanged event you add, never trigger, that is, TextChanged event failed.

The above is just helping everyone understand, because this can be more than half of the power, especially the key, you can save a lot of strength. I hope expert criticism

转载请注明原文地址:https://www.9cbs.com/read-25909.html

New Post(0)