c# - Check if there are any subscribers to an event handler whilst using a custom delegate -


so idea simple, how check if there duplicate subscribers.

so if have

_moduleevent += _coachesevents.ondifficultyscorelistchanged;

subscribed in multiple locations, don't want method called twice (or more).

my current code (with added logic after researching):

        /// <summary>         /// delegate points multiple module methods         /// </summary>         /// <param name="sender"></param>         /// <param name="args"></param>         private delegate void moduleeventhandler(object sender, opportunityeventargs args);          /// <summary>         ///          /// </summary>         private event moduleeventhandler _moduleevent         {             add             {                 if (_moduleevent == null || !_moduleevent.getinvocationlist().contains(value))                 {                     _moduleevent += value;                 }             }             remove             {                 _moduleevent -= value;             }         } 

in event checking if it's null , checking getinnvocationlist, error _moduleevent should appear on left hand side of += etc.

        /// <summary>         /// check whether there subscribers         /// </summary>         private void _onmoduleevent(businessentities.opportunity o)         {             if (_moduleevent != null)                 _moduleevent(this, new opportunityeventargs() { opportunity = o });         } 

i see _moduleevent should appear on left hand side of += error above.

i can assume it's custom delegate ? if remove add/remove in eventhandler works fine.

any appreciated.

i suggest should create dictionary , check

create class below

public class subscription {   public readonly methodinfo methodinfo;   public readonly weakreference targetobjet;    public subscription(action<tmessage> action, eventaggregator eventaggregator)   {       methodinfo = action.method;       targetobjet = new weakreference(action.target);   } } 

than use class in class expose event

public class eventexposer {   private dictionary<type, ilist> subscriber;    public eventexposer()   {             subscriber = new dictionary<type, ilist>();   }     public void addevent(action action)    {             type t = typeof(action.target);             if (!subscriber.containskey(t))             {                     subscriber.add(t, action);             }    }  } 

Comments