Вопрос по c#, .net, events – C #: событие с простотой добавить / удалить! = Типичное событие?

40

Error: User Rate Limit Exceeded

public delegate void EventHandler();

Error: User Rate Limit Exceeded

public static void RaiseEvent(this EventHandler self)        {
   if (self != null) self.Invoke();
}

Error: User Rate Limit Exceeded

public event EventHandler TypicalEvent;

Error: User Rate Limit Exceeded

TypicalEvent.RaiseEvent();

Error: User Rate Limit Exceeded

private EventHandler _explicitEvent;
public event EventHandler ExplicitEvent {
   add { _explicitEvent += value; } 
   remove { _explicitEvent -= value; }
}

Error: User Rate Limit Exceeded

ExplicitEvent.RaiseEvent(); //RaiseEvent() does not exist on the event for some reason

Error: User Rate Limit Exceeded

The event 'ExplicitEvent' can only appear on the left hand side of += or -=

Why should an event defined using the typical syntax be different from an event defined using the explicit add/remove syntax and why extension methods do not work on the latter?

EDIT: I found I can work around it by using the private event handler directly:

_explicitEvent.RaiseEvent();

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded IllidanS4

Ваш Ответ

4   ответа
8

private EventHandler _explicitEvent;
public event EventHandler ExplicitEvent {
   add { _explicitEvent += value; } 
   remove { _explicitEvent -= value; }
}

private double seconds; 
public double Hours
{
    get { return seconds / 3600; }
    set { seconds = value * 3600; }
}

private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private EventHandler _explicitEvent;
public event EventHandler ExplicitEvent 
{
   add 
   { 
       if (_explicitEvent == null) timer.Start();
       _explicitEvent += value; 
   } 
   remove 
   { 
      _explicitEvent -= value; 
      if (_explicitEvent == null) timer.Stop();
   }
}

35

private EventHandler _explicitEvent_A;
private EventHandler _explicitEvent_B;
private bool flag;
public event EventHandler ExplicitEvent {
   add {
         if ( flag = !flag ) { _explicitEvent_A += value; /* or do anything else */ }
         else { _explicitEvent_B += value; /* or do anything else */ }
   } 
   remove {
         if ( flag = !flag ) { _explicitEvent_A -= value; /* or do anything else */ }
         else { _explicitEvent_B -= value; /* or do anything else */ }
   }
}

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1

be

_explicitEvent.RaiseEvent()
Error: User Rate Limit ExceededstillError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
50

Error: User Rate Limit Exceeded

public event EventHandler Foo;

Error: User Rate Limit ExceededandError: User Rate Limit ExceededFooError: User Rate Limit ExceededfieldError: User Rate Limit ExceededFooError: User Rate Limit ExceededotherError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

public EventHandler TypicalEvent;

Error: User Rate Limit ExceededeventError: User Rate Limit ExceededfieldError: User Rate Limit ExceededEventHandler. AnyoneError: User Rate Limit ExceededstringError: User Rate Limit ExceededintError: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceededstackoverflow.com/questions/11180068/…Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы