vb.net - AddHandler only if no handlers for this event? -


I want to set an event handler only if it is not set:

  If GetHandlers (MyWindow.Closed, AddressOf MyWindow_Closed) .length = 0 then AddHandler MyWindow.Closed, MyWindow_Closed andIf  

You can not really query the present value of the event's representative, except the code that defines event is your intention here? Generally you should not be very worried (urgent) with other members? There are ways to hack find before the encapsulation, but they are not recommended (this is not just a good idea).

If you are worried that you are dealing with that event before that handler (i.e. you do not want to subscribe again, you always have to either : C code can not be it, or B: cheat (C # example):

  // removed handler ** if subscribed, then mine Subscribe to Windows Clause - = MyvindOvclosed; Myvando.Close + = MyWindowload;  

Awkward In order to get the list ... brittle but possible.In simple cases you can use reflection to get the field, and stop the price. Uses sparse techniques (for customers without events) To reduce the space). In the case of FormClosed , this has been done through EVENT_FORMCLOSED .

More understanding with examples #, Sorry):

  form form = new form (); Form.FormClosed + = Representative {Console.WriteLine ("a");}; // just some, any form. Formclosed + = representative {console.light line ("b");}; // just a few, some thing key = typef (form) .GetField ("EVENT_FORMCLOSED", binding fulges. Nonpolic | binding fogs. Static) gate value (empty); EventHandler List Events = (Event Handler List) Type (Component) .GetProperty ("Events", Binding Falgues. NonPolic | BindingFlag.Instance) .GetValue (form, blank); FormClosedEventHandler Handler = (FormClosedEventHandler) events [key]; Foreign currency (formulated event handler in sub-Thunderbolter gate invoicesist ()) {Subhädler (form, blank); // Access two events separately)  

In the case of an view T & gt; , the representative is directly on a field, so less indecision is required:

  viewCollection & lt; SomeType & gt; List = ... NotifyCollectionChangedEventHandler handler = (NotifyCollectionChangedEventHandler) list.GetType () .GetField ("Change the archive", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue (list);  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -