You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. namespace SUISS.Cloud.Extensions
  6. {
  7. public static class IDictionaryExtensions
  8. {
  9. public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
  10. {
  11. TValue result;
  12. if (dictionary.TryGetValue(key, out result))
  13. {
  14. return result;
  15. }
  16. return defaultValue;
  17. }
  18. public static string GetString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string defaultValue)
  19. {
  20. TValue tvalue;
  21. if (dictionary.TryGetValue(key, out tvalue))
  22. {
  23. string result = defaultValue;
  24. try
  25. {
  26. result = Convert.ToString(tvalue, CultureInfo.InvariantCulture);
  27. }
  28. catch (Exception exception)
  29. {
  30. UnityEngine.Debug.LogException(exception);
  31. }
  32. return result;
  33. }
  34. return defaultValue;
  35. }
  36. public static int GetInt<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, int defaultValue)
  37. {
  38. TValue tvalue;
  39. if (dictionary.TryGetValue(key, out tvalue))
  40. {
  41. int result = defaultValue;
  42. try
  43. {
  44. result = Convert.ToInt32(tvalue, CultureInfo.InvariantCulture);
  45. }
  46. catch (Exception exception)
  47. {
  48. UnityEngine.Debug.LogException(exception);
  49. }
  50. return result;
  51. }
  52. return defaultValue;
  53. }
  54. public static long GetLong<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, long defaultValue)
  55. {
  56. TValue tvalue;
  57. if (dictionary.TryGetValue(key, out tvalue))
  58. {
  59. long result = defaultValue;
  60. try
  61. {
  62. result = Convert.ToInt64(tvalue, CultureInfo.InvariantCulture);
  63. }
  64. catch (Exception exception)
  65. {
  66. UnityEngine.Debug.LogException(exception);
  67. }
  68. return result;
  69. }
  70. return defaultValue;
  71. }
  72. public static bool GetBoolean<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, bool defaultValue)
  73. {
  74. TValue tvalue;
  75. if (dictionary.TryGetValue(key, out tvalue))
  76. {
  77. bool result = defaultValue;
  78. try
  79. {
  80. result = Convert.ToBoolean(tvalue, CultureInfo.InvariantCulture);
  81. }
  82. catch (Exception exception)
  83. {
  84. UnityEngine.Debug.LogException(exception);
  85. }
  86. return result;
  87. }
  88. return defaultValue;
  89. }
  90. public static void RemoveAll<Key, Value>(this IDictionary<Key, Value> dict, Predicate<KeyValuePair<Key, Value>> predicate)
  91. {
  92. List<Key> list = new List<Key>();
  93. foreach (KeyValuePair<Key, Value> obj in dict)
  94. {
  95. if (predicate(obj))
  96. {
  97. list.Add(obj.Key);
  98. }
  99. }
  100. foreach (Key key in list)
  101. {
  102. dict.Remove(key);
  103. }
  104. }
  105. }
  106. }