Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reference an event in C#
    primarykey
    data
    text
    <p>I have the following class, which has one public event called <code>LengthChanged</code>:</p> <pre><code>class Dimension { public int Length { get { return this.length; } set { if (this.length != value) { this.length = value; this.OnLengthChanged (); } } protected virtual void OnLengthChanged() { var handler = this.LengthChanged; if (handler != null) { handler (this, System.EventArgs.Empty); } } public event System.EventHandler LengthChanged; private int length; } </code></pre> <p>I would like to be able to register/unregister handlers for this event in a method called <code>Observer</code>, which does not know anything about the <code>Dimension</code> class. I have come up with two scenarios, none of which are really satisfying:</p> <ol> <li><p>Define an interface <code>ILengthChanged</code> with the <code>LengthChanged</code> event, then make sure <code>Dimension</code> implements <code>ILengthChanged</code>. Then I have to provide one implementation of the <code>Observer</code> method for every interface I define. This by no way generic enough. I'd really want to be able to simply pass in a reference to a <code>System.EventHandler</code> event.</p></li> <li><p>Use <code>System.Action&lt;System.EventHandler&gt;</code> callbacks for registering and unregistering the event handler in the <code>Observer</code> method, just like that:</p> <p>class Foo { public void Observer(System.Action register, System.Action unregister) { register (this.MyEventHandler);</p> <pre><code> // keep track of the unregister callback, so that we can unregister // our event handler later on, if needed... } private void MyEventHandler(object sender, System.EventArgs e) { ... } </code></pre> <p>}</p></li> </ol> <p>which would then be invoked like this:</p> <pre><code>Foo foo = ...; Dimension dim = ...; foo.Observer (x =&gt; dim.LengthChanged += x, x =&gt; dim.LengthChanged -= x); </code></pre> <p>and which, when executed, will indeed end up wiring the <code>LengthChanged</code> event with the internal event handler <code>MyEventHandler</code>. But this is not very elegant. I would have loved to be able to write this instead:</p> <pre><code>Foo foo = ...; Dimension dim = ...; foo.Observer (dim.LengthChanged); </code></pre> <p>but I've no idea how this could be achieved. Maybe I am missing something really obvious here? I guess that some <code>dynamic</code> magic could do the trick, somehow, but this would not enforce compile-time type checking: I don't want the users of <code>Observer</code> to pass in references to events which do not satisfy the <code>System.EventHandler</code> event signature.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload