SerializableArray.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. #pragma warning disable 649
  6. /// <summary>
  7. /// Allows you to access meta-data about a managed array and its children. Similar to Reflection but simpler and faster.
  8. /// </summary>
  9. public sealed class SerializableArray : ScriptObject
  10. {
  11. private SerializableProperty.FieldType elementPropertyType;
  12. private Type elementType;
  13. private SerializableProperty parentProperty;
  14. /// <summary>
  15. /// Type of serializable property used for the elements stored in the array.
  16. /// </summary>
  17. public SerializableProperty.FieldType ElementPropertyType
  18. {
  19. get { return elementPropertyType; }
  20. }
  21. /// <summary>
  22. /// Type used for the elements stored in the array.
  23. /// </summary>
  24. public Type ElementType
  25. {
  26. get { return elementType; }
  27. }
  28. /// <summary>
  29. /// Constructor for use by the runtime only.
  30. /// </summary>
  31. /// <param name="elementType">C# type of the elements in the array.</param>
  32. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  33. private SerializableArray(Type elementType, SerializableProperty parentProperty)
  34. {
  35. this.parentProperty = parentProperty;
  36. this.elementType = elementType;
  37. elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
  38. }
  39. /// <summary>
  40. /// Returns a serializable property for a specific array element.
  41. /// </summary>
  42. /// <param name="elementIdx">Index of the element in the array.</param>
  43. /// <returns>Serializable property that allows you to manipulate contents of the array entry.</returns>
  44. public SerializableProperty GetProperty(int elementIdx)
  45. {
  46. SerializableProperty.Getter getter = () =>
  47. {
  48. Array array = parentProperty.GetValue<Array>();
  49. if (array != null)
  50. return array.GetValue(elementIdx);
  51. else
  52. return null;
  53. };
  54. SerializableProperty.Setter setter = (object value) =>
  55. {
  56. Array array = parentProperty.GetValue<Array>();
  57. if(array != null)
  58. array.SetValue(value, elementIdx);
  59. };
  60. SerializableProperty property = Internal_CreateProperty(mCachedPtr);
  61. property.Construct(ElementPropertyType, elementType, getter, setter);
  62. return property;
  63. }
  64. /// <summary>
  65. /// Returns number of elements in the array.
  66. /// </summary>
  67. /// <returns>Number of elements in the array.</returns>
  68. public int GetLength()
  69. {
  70. Array array = parentProperty.GetValue<Array>();
  71. if (array != null)
  72. return array.GetLength(0); // TODO - Support multi-rank arrays
  73. else
  74. return 0;
  75. }
  76. [MethodImpl(MethodImplOptions.InternalCall)]
  77. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  78. }
  79. }