Skip to content

Instantly share code, notes, and snippets.

@LoganBarnett
Created June 21, 2011 17:09
Show Gist options
  • Save LoganBarnett/1038341 to your computer and use it in GitHub Desktop.
Save LoganBarnett/1038341 to your computer and use it in GitHub Desktop.
Demonstrates callbacks inside events using Action<T> or Func<T>
public delegate void CreateThingHandler(Func<Thing, Thing> preCreate, Thing thing);
public class Thing
{
public static event CreateThingHandler OnMadeThing;
public bool Valid { get; set;}
List<Thing> things = new List<thing>();
public static Thing MakeThing(Func<Thing, Thing> func)
{
var thing = new Thing();
thing = OnMadeThing(func, thing);
things.Add(thing);
return thing;
}
}
//// Usage
void ValidateThing(Func<Thing, Thing> func, Thing thing)
{
return func(thing);
}
Thing.OnMadeThing += ValidateThing;
thing = Thing.MakeThing(t => t.Valid = true);
thing.Valid // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment