Last active
August 29, 2015 14:06
-
-
Save lantoli/e901546973b9486e0b9a to your computer and use it in GitHub Desktop.
Leo Antoli Solution to reto 2 MSDN defining add and remove http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Leo Antoli solution to reto 2 MSDN using add and remove: http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx | |
// NOTE: It's not thread-safe, locking should be added. | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Reto2ClassLibrary | |
{ | |
public class Reto2 : IReto2 | |
{ | |
private EventHandler handler; | |
public event EventHandler EventFired | |
{ | |
add { | |
if (value.Target is Item) { | |
var list = handler == null ? new List<EventHandler>() | |
: handler.GetInvocationList().OfType<EventHandler>().ToList(); | |
foreach (var evt in list) { | |
handler -= evt; | |
} | |
list.Add(value); | |
foreach (var evt in list.OrderBy(x => (x.Target as Item).Index)) { | |
handler += evt; | |
} | |
} | |
} | |
remove { | |
handler -= value; | |
} | |
} | |
public void FireEvent() { | |
if (handler != null) { | |
handler(this, EventArgs.Empty); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment