SerializableList.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 elementType;
  13. private Type internalElementType;
  14. private SerializableProperty parentProperty;
  15. /// <summary>
  16. /// Type of elements stored in the list.
  17. /// </summary>
  18. public SerializableProperty.FieldType ElementType
  19. {
  20. get { return elementType; }
  21. }
  22. /// <summary>
  23. /// Constructor for use by the runtime only.
  24. /// </summary>
  25. /// <param name="internalElementType">C# type of the elements in the list.</param>
  26. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  27. private SerializableList(Type internalElementType, SerializableProperty parentProperty)
  28. {
  29. this.parentProperty = parentProperty;
  30. this.internalElementType = internalElementType;
  31. elementType = SerializableProperty.DetermineFieldType(internalElementType);
  32. }
  33. /// <summary>
  34. /// Returns a serializable property for a specific list element.
  35. /// </summary>
  36. /// <param name="elementIdx">Index of the element in the list.</param>
  37. /// <returns>Serializable property that allows you to manipulate contents of the list entry.</returns>
  38. public SerializableProperty GetProperty(int elementIdx)
  39. {
  40. SerializableProperty.Getter getter = () =>
  41. {
  42. IList list = parentProperty.GetValue<IList>();
  43. if (list != null)
  44. return list[elementIdx];
  45. else
  46. return null;
  47. };
  48. SerializableProperty.Setter setter = (object value) =>
  49. {
  50. IList list = parentProperty.GetValue<IList>();
  51. if (list != null)
  52. list[elementIdx] = value;
  53. };
  54. SerializableProperty property = Internal_CreateProperty(mCachedPtr);
  55. property.Construct(ElementType, internalElementType, getter, setter);
  56. return property;
  57. }
  58. /// <summary>
  59. /// Returns number of elements in the list.
  60. /// </summary>
  61. /// <returns>Number of elements in the list.</returns>
  62. public int GetLength()
  63. {
  64. IList list = parentProperty.GetValue<IList>();
  65. if (list != null)
  66. return list.Count;
  67. else
  68. return 0;
  69. }
  70. [MethodImpl(MethodImplOptions.InternalCall)]
  71. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  72. }
  73. }