Use reflection to clear all delegate hanging on the event

Source: Internet
Author: User
In. net, event is a very useful feature, which allows us to easily implement callback. But it also has a lot of inconvenience: An event can only use the + =/-= Operator to increase or decrease delegate except the class that declares it, and cannot delete all delegate hanging on it at a time.
Program When the scale is small, it is easy to do. Once the program expands, it is very troublesome to find out the delegate hanging on an event. In particular, many classes have events, and many events are linked in different places. When determining whether an object is unnecessary, You need to promptly disconnect the delegate from its events.
The following function can directly empty all the events of an object, Code I will not explain it much: 1 ///   <Summary>
2 /// Clears the delegate associated with all events of an object.
3 ///   </Summary>
4 ///   <Param name = "objecthasevents"> Objects with events </Param>
5 Public   Static   Void Clearallevents ( Object Objecthasevents)
6 {
7 If (Objecthasevents =   Null )
8 {
9 Return ;
10 }
11
12 Eventinfo [] Events = Objecthasevents. GetType (). getevents (
13 Bindingflags. Public |
14 Bindingflags. nonpublic |
15 Bindingflags. instance );
16
17 If (Events =   Null   | Events. Length <   1 )
18 {
19 Return ;
20 }
21
22 For ( Int I =   0 ; I < Events. length; I ++ )
23 {
24 Try
25 {
26 Eventinfo EI = Events [I];
27
28 /* **************************************** ***************
29 * Each event of the class corresponds to a private delegate class with the same name.
30 * Member variable (this can be confirmed by reflector ). Because private
31 * Member variables cannot be modified in the base class.
32 * Events declared in class must be obtained from the declaringtype of eventinfo.
33 * The fieldinfo of the member variable corresponding to the event is modified.
34 **************************************** *************** */
35 Fieldinfo fi =
36 EI. declaringtype. getfield (Ei. Name,
37 Bindingflags. nonpublic |
38 Bindingflags. instance );
39 If (Fi ! =   Null )
40 {
41 // Set the field corresponding to the event to null to clear all delegate hooks on the event.
42 Fi. setvalue (objecthasevents, Null );
43 }
44 }
45 Catch
46 {
47 }
48 }
49 }
50
51

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.