Skip to content

Instantly share code, notes, and snippets.

@rsciriano
Last active August 29, 2015 14:06
Show Gist options
  • Save rsciriano/66a292f1efa2149cd78b to your computer and use it in GitHub Desktop.
Save rsciriano/66a292f1efa2149cd78b to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
namespace Reto2ClassLibrary
{
/// <summary>
/// Solución al reto 2 MSDN http://blogs.msdn.com/b/esmsdn/archive/2014/09/19/retosmsdn-reto-2-161-esos-eventos.aspx
/// </summary>
public class Reto2 : IReto2
{
// Lista ordenada de delegados
private SortedList<int, System.EventHandler> listEventDelegates;
public Reto2()
{
listEventDelegates = new SortedList<int, System.EventHandler>();
}
/// <summary>
/// Evento basado en propiedad para capturar Add/Remove
/// </summary>
/// <remarks>
/// How to: Handle Multiple Events Using Event Properties
/// http://msdn.microsoft.com/en-us/library/8843a9ch(v=vs.110).aspx
/// </remarks>
public event System.EventHandler EventFired
{
// Add the input delegate to the collection.
add
{
Item item = value.Target as Item;
// Cuando el evento es un Item, lo añadimos al diccionario
if (item != null)
{
listEventDelegates.Add(item.Index, value);
}
}
// Remove the input delegate from the collection.
remove
{
Item item = value.Target as Item;
// Cuando el evento es un Item, lo eliminamos del diccionario
if (item != null)
{
listEventDelegates.Remove(item.Index);
}
}
}
public void FireEvent()
{
foreach (var item in listEventDelegates)
{
item.Value("Reto2", new System.EventArgs());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment