SerializableDictionary.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. #pragma warning disable 649
  8. /// <summary>
  9. /// Allows you to access meta-data about a managed dictionary and its children. Similar to Reflection but simpler and
  10. /// faster.
  11. /// </summary>
  12. public sealed class SerializableDictionary : ScriptObject
  13. {
  14. private SerializableProperty.FieldType keyPropertyType;
  15. private SerializableProperty.FieldType valuePropertyType;
  16. private Type keyType;
  17. private Type valueType;
  18. private SerializableProperty parentProperty;
  19. /// <summary>
  20. /// Type of serializable property used for the keys stored in the dictionary.
  21. /// </summary>
  22. public SerializableProperty.FieldType KeyPropertyType
  23. {
  24. get { return keyPropertyType; }
  25. }
  26. /// <summary>
  27. /// Type of serializable property used for the values stored in the dictionary.
  28. /// </summary>
  29. public SerializableProperty.FieldType ValuePropertyType
  30. {
  31. get { return valuePropertyType; }
  32. }
  33. /// <summary>
  34. /// Type used for of keys stored in the dictionary.
  35. /// </summary>
  36. public Type KeyType
  37. {
  38. get { return keyType; }
  39. }
  40. /// <summary>
  41. /// Type used for values stored in the dictionary.
  42. /// </summary>
  43. public Type ValueType
  44. {
  45. get { return valueType; }
  46. }
  47. /// <summary>
  48. /// Constructor for use by the runtime only.
  49. /// </summary>
  50. /// <param name="keyType">C# type of the keys in the dictionary.</param>
  51. /// <param name="valueType">C# type of the values in the dictionary.</param>
  52. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  53. private SerializableDictionary(Type keyType, Type valueType, SerializableProperty parentProperty)
  54. {
  55. this.parentProperty = parentProperty;
  56. this.keyType = keyType;
  57. this.valueType = valueType;
  58. keyPropertyType = SerializableProperty.DetermineFieldType(keyType);
  59. valuePropertyType = SerializableProperty.DetermineFieldType(valueType);
  60. }
  61. /// <summary>
  62. /// Returns a serializable property for the specified entry.
  63. /// </summary>
  64. /// <param name="key">Dictionary key for the value to retrieve.</param>
  65. /// <returns>Serializable property that allows you to manipulate contents of the dictionary entry.</returns>
  66. public KeyValuePair<SerializableProperty, SerializableProperty> GetProperty(object key)
  67. {
  68. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  69. if (dictionary == null || !dictionary.Contains(key))
  70. return new KeyValuePair<SerializableProperty, SerializableProperty>(null, null);
  71. SerializableProperty keyProperty;
  72. {
  73. SerializableProperty.Getter getter = () => key;
  74. SerializableProperty.Setter setter = (object value) => {};
  75. keyProperty = Internal_CreateKeyProperty(mCachedPtr);
  76. keyProperty.Construct(KeyPropertyType, keyType, getter, setter);
  77. }
  78. SerializableProperty valueProperty;
  79. {
  80. SerializableProperty.Getter getter = () =>
  81. {
  82. IDictionary dict = parentProperty.GetValue<IDictionary>();
  83. if (dict != null)
  84. return dict[key];
  85. else
  86. return null;
  87. };
  88. SerializableProperty.Setter setter = (object value) =>
  89. {
  90. IDictionary dict = parentProperty.GetValue<IDictionary>();
  91. if (dict != null)
  92. dict[key] = value;
  93. };
  94. valueProperty = Internal_CreateValueProperty(mCachedPtr);
  95. valueProperty.Construct(ValuePropertyType, valueType, getter, setter);
  96. }
  97. return new KeyValuePair<SerializableProperty, SerializableProperty>(keyProperty, valueProperty);
  98. }
  99. /// <summary>
  100. /// Returns the total number of elements in the dictionary.
  101. /// </summary>
  102. /// <returns>Total number of elements in the dictionary.</returns>
  103. public int GetLength()
  104. {
  105. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  106. if (dictionary != null)
  107. return dictionary.Count;
  108. else
  109. return 0;
  110. }
  111. [MethodImpl(MethodImplOptions.InternalCall)]
  112. private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
  115. }
  116. }