c# - Unsubscribe from original event when try unsubscribe from a variable of that EventHandler -


i'm trying unsubscribe attached method event. have code:

public event eventhandler menuitemselectionchanged;  var eh = menuitemselectionchanged; eh -= themethod; 

after run above code result is:

menuitemselectionchanged: not null

el: null

but want unsubscribe themethod menuitemselectionchanged, using eh (a pointer eventhandler). how can use eh remove themethod eh , menuitemselectionchanged

i need result:

menuitemselectionchanged: null

el: null

my main scenario is:

inside targetclass.cs:

    public event eventhandler menuitemselectionchanged;      public void unsubscribefromevent(thisclasseventhandlernames eventhandlername, dictionary<frameworkelement, methodinfo> eventhandlerinfos)     {         var eh = geteventhandler(eventhandlername);         if (eh == null) return;          foreach (var eventhandlerinfo in eventhandlerinfos)         {             var info = eventhandlerinfo;              if (eh == null)                 break;                  var invocationlist = eh.getinvocationlist().where(x =>                     equals(x.target, info.key) &&                     x.method == info.value);              foreach (var mydeligate in invocationlist)             {                 if (eh == null)                     break;                  // have problem here>>                 eh -= (eventhandler) mydeligate;                 // result: eh null menuitemselectionchanged not null             }         }     }      public enum thisclasseventhandlernames     {         menuitemselectionchanged     }      private eventhandler geteventhandler(thisclasseventhandlernames eventhandlername)     {         eventhandler result = null;         switch (eventhandlername)         {             case thisclasseventhandlernames.menuitemselectionchanged:             {                 result = menuitemselectionchanged;                 break;             }             default:             {                 result = null;                 break;             }         }         return result;     } 

i call above code in wpf user control codes this:

inside myusercontrol.cs:

targetclassobject.unsubscribefromevent(     targetclass.thisclasseventhandlernames.menuitemselectionchanged,     new dictionary<frameworkelement, methodinfo>     {         {             this,             infrastructure.utility.getmethodinfo<myusercontrol>(x => x.menucontrol_menuitemselectionchanged(null, null))         }     }); 

really want unsubscribe method event call method inside class (targetclass) parameters:

a) enum value (to event it)

b) method info (to unsubscribe method event)

note: platform c#, wpf, .net framework 4.5

when move menuitemselectionchanged variable , assigning variable, aren't changing original delegate's invocation list. eh -= themethod; same eh = eh - themethod; (if inside class), produces new multicastdelegate , doesn't modify original one.

so answer first code:

public event eventhandler menuitemselectionchanged;  var eh = menuitemselectionchanged; eh -= themethod; menuitemselectionchanged = eh; 

however, want change event dynamically, either can add second method gets thisclasseventhandlernames , eventhandler , assigns back. or can modify geteventhandler method return reference handler:

private delegate void handlermodifier(ref eventhandler handler); private bool modifyeventhandler(thisclasseventhandlernames eventhandlername, handlermodifier mod) {     switch (eventhandlername)     {         case thisclasseventhandlernames.menuitemselectionchanged:         {             mod(ref menuitemselectionchanged);             return true;         }         default:         {             return false;         }     } }  public void unsubscribefromevent(thisclasseventhandlernames eventhandlername, dictionary<frameworkelement, methodinfo> eventhandlerinfos) {     modifyeventhandler(         eventhandlername,         delegate(ref eventhandler eh)         {             if (eh == null) return;              foreach (var eventhandlerinfo in eventhandlerinfos)             {                 var info = eventhandlerinfo;                  if (eh == null)                     break;                      var invocationlist = eh.getinvocationlist().where(x =>                         equals(x.target, info.key) &&                         x.method == info.value);                  foreach (var mydeligate in invocationlist)                 {                     if (eh == null)                         break;                      // have problem here>>                     eh -= (eventhandler) mydeligate;                     // result: eh null menuitemselectionchanged not null                 }             }         }     );  } 

modifyeventhandler "returns" reference handler, changes passed menuitemselectionchanged.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -