Last active
June 25, 2019 16:23
-
-
Save jakehawken/03aebd639b065f1a0ff19e334259345f to your computer and use it in GitHub Desktop.
Getting that sweet, sweet `didSet` functionality in C#.
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
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| public class PropertyBox<T> | |
| { | |
| private T value; | |
| public Action<T> didSet; | |
| public Action<T,T> willSet; | |
| public T get() { | |
| return value; | |
| } | |
| public static PropertyBox<Q> withValue<Q>(Q val) { | |
| return new PropertyBox<Q>() { value = val }; | |
| } | |
| public void set(T val) { | |
| if (willSet != null) { | |
| willSet(value, val); | |
| } | |
| value = val; | |
| if (didSet != null) { | |
| didSet(val); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment