SerializableProperty.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BansheeEngine
  6. {
  7. public sealed class SerializableProperty
  8. {
  9. internal delegate object Getter();
  10. internal delegate void Setter(object value);
  11. private SerializableField.FieldType type;
  12. private Type internalType;
  13. private Getter getter;
  14. private Setter setter;
  15. internal SerializableProperty(SerializableField.FieldType type, Type internalType, Getter getter, Setter setter)
  16. {
  17. this.type = type;
  18. this.internalType = internalType;
  19. this.getter = getter;
  20. this.setter = setter;
  21. }
  22. public SerializableField.FieldType Type
  23. {
  24. get { return type; }
  25. }
  26. public T GetValue<T>()
  27. {
  28. if (!typeof(T).IsAssignableFrom(internalType))
  29. throw new Exception("Attempted to retrieve a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  30. return (T)getter();
  31. }
  32. public void SetValue<T>(T value)
  33. {
  34. if (!typeof(T).IsAssignableFrom(internalType))
  35. throw new Exception("Attempted to set a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  36. setter(value);
  37. }
  38. public SerializableObject GetObject()
  39. {
  40. if (type != SerializableField.FieldType.Object)
  41. throw new Exception("Attempting to retrieve object information from a field that doesn't contain an object.");
  42. return new SerializableObject(GetValue<object>());
  43. }
  44. public SerializableArray GetArray()
  45. {
  46. if (type != SerializableField.FieldType.Array)
  47. throw new Exception("Attempting to retrieve array information from a field that doesn't contain an array.");
  48. return new SerializableArray(GetValue<Array>());
  49. }
  50. }
  51. }