I want to expose properties/fields in my classes to an UI system, the UI components will subscribe to each field onChange event to receive an event when the field has changed to update the UI.
What I've done so far is make a wrapper for any type that has a property value that triggers an event when its set:
public class LogicClass
{
public Synced<int> stuff;
static void Main(string[] args)
{
// Then in the UI code we will have:
// logicClass.stuff.onChange += HandleStuffChange;
}
}
public class Synced<T>
{
public event OnChange onChange;
public delegate void OnChange(Synced<T> value);
private T _value;
public T value
{
get => _value;
set
{
_value = value;
onChange?.Invoke(this);
}
}
}
The problem is that its kind of annoying to refer to the value property from the wrapper every time you want to modify it. Is there a way to make this more transparent from the clients perspective (LogicClass in the example) ? Reflection is acceptable too.