2
0

SerializableList.cs 3.4 KB

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