1

I have a generic event raising method in my events class to be used with all events

    protected virtual void RaiseEvent<T>(EventHandler<T> eventToRaise, T eventArgs) where T : EventArgs
    {
        if (eventToRaise != null)
        {
            eventToRaise(this, eventArgs);
        }
    }

but when I try to call this method from a subclass , I get an error that event can be used just on the left side of += or -= expression . is there any way to make this work ?

Update : one idea is to use enums . first define an enum of event names . then adding a dictionary that maps each enum value to its corresponding event . and finally in the method get the event and raise it based on what the parameter is . but I'm not sure how the dictionary should be ?

 public enum Events {Insert , Update , Delete};

dictionary<Events,EventHandler<T>> EventsDic = new dictionary<Events,EventHandler<T>>;

EventsDic.Add(Events.Insert,this.Insert);

is this correct ?

mohsen dorparasti
  • 8,107
  • 7
  • 41
  • 61

0 Answers0