Created
June 21, 2011 17:09
-
-
Save LoganBarnett/1038341 to your computer and use it in GitHub Desktop.
Demonstrates callbacks inside events using Action<T> or Func<T>
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
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