Last active
August 4, 2016 17:48
-
-
Save WillzZz/e188d3f42a9b87b20716 to your computer and use it in GitHub Desktop.
Bindable
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; | |
public class Bindable<T> | |
{ | |
private T _value; | |
public T Value | |
{ | |
get { return _value; } | |
set | |
{ | |
_value = value; | |
OnUpdated(value); | |
} | |
} | |
public Action<T> OnUpdated = delegate { }; | |
public Bindable(T value) | |
{ | |
Value = value; | |
} | |
public static explicit operator Bindable<T>(T t) | |
{ | |
return new Bindable<T>(t); | |
} | |
public static implicit operator T(Bindable<T> bindable) | |
{ | |
return bindable.Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yup all of this is correct. Sorry I haven't updated this in a while. I've swapped to an explicit operator and letting Value be public. It's not possible to do an extension implicit operator in the current version of C#, although it could happen in the future. Ultimately that's what we want (We want a ref to the Bindable class and assign a value).
It's all syntactic sugar that I wanted, but couldn't quite achieve. I'll update the gist.