SerializableList.cs 3.1 KB

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