title: Defining/raising a generic event using constraints
body: <p>I want to raise an event that will allow an object of type <code>Widget</code> (or any derived class) to be returned, with the specific type being defined via generics.</p>
  <pre><code>public class WidgetProcessor
  {
     public event EventHandler&lt;WidgetRequiredEventArgs&lt;Widget&gt;&gt; WidgetRequired;
  
     public void DoSomethingThatNeedsAWidget&lt;T&gt;() where T: Widget
     {
        Widget widget = OnWidgetRequired&lt;T&gt;();
  
        //...now do something with the widget
     }
  
     private T OnWidgetRequired&lt;T&gt;() where T: Widget
     {
        T widget = null;
  
        if (this.WidgetRequired != null)
        {
           WidgetRequiredEventArgs&lt;T&gt; e = new WidgetRequiredEventArgs&lt;T&gt;();
           this.WidgetRequired(this, e);
  
           widget = e.Widget;
        }
  
        return widget;
     }
  }
  
  public class WidgetRequiredEventArgs&lt;T&gt;
     : EventArgs where T : Widget
  {
     public WidgetRequiredEventArgs()
     {
     }
  
     public T Widget { get; set; }
  }
  </code></pre>
  <p>Constraints on <code>OnWidgetRequired&lt;T&gt;()</code> and <code>DoSomethingThatNeedsAWidget&lt;T&gt;()</code> allow me to limit the specified type to <code>Widget</code> or a derived class.  Ideally, I'd do the same for the event declaration, but it doesn't support the use of <code>&lt;T&gt;</code> with a constraint for the event args, so I've had to declare it explicitly as <code>Widget</code>.</p>
  <p>However, this gives the compile-time error:</p>
  <blockquote>
  <p>CS1503: Argument 2: cannot convert from 'WidgetRequiredEventArgs&lt;T&gt;' to 'WidgetRequiredEventArgs&lt;Widget&gt;'</p>
  </blockquote>
  <p>for the <code>e</code> argument on the line:</p>
  <pre><code>this.WidgetRequired(this, e);
  </code></pre>
  <p>So why don't the constraints on <code>OnWidgetRequired&lt;T&gt;()</code> and <code>WidgetRequiredEventArgs&lt;T&gt;</code> satisfy the eventhandler's type definition, and how can I get it to compile?</p>