SerializableValue.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BansheeEngine
  7. {
  8. public sealed class SerializableValue
  9. {
  10. internal delegate void Setter(object value);
  11. internal delegate object Getter();
  12. private Type type;
  13. private Setter setter;
  14. private Getter getter;
  15. internal SerializableValue(Type type, Getter getter, Setter setter)
  16. {
  17. this.type = type;
  18. this.getter = getter;
  19. this.setter = setter;
  20. }
  21. public T GetValue<T>()
  22. {
  23. if (typeof (T) != type)
  24. throw new Exception("Attempted to retrieve a serializable value using an invalid type.");
  25. return (T) getter();
  26. }
  27. public void SetValue<T>(T value)
  28. {
  29. if (typeof(T) != type)
  30. throw new Exception("Attempted to set a serializable value using an invalid type.");
  31. setter(value);
  32. }
  33. }
  34. }