DictionaryExtensions.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Abp.Collections.Extensions
  4. {
  5. /// <summary>
  6. /// Extension methods for Dictionary.
  7. /// </summary>
  8. public static class DictionaryExtensions
  9. {
  10. /// <summary>
  11. /// This method is used to try to get a value in a dictionary if it does exists.
  12. /// </summary>
  13. /// <typeparam name="T">Type of the value</typeparam>
  14. /// <param name="dictionary">The collection object</param>
  15. /// <param name="key">Key</param>
  16. /// <param name="value">Value of the key (or default value if key not exists)</param>
  17. /// <returns>True if key does exists in the dictionary</returns>
  18. internal static bool TryGetValue<T>(this IDictionary<string, object> dictionary, string key, out T value)
  19. {
  20. object valueObj;
  21. if (dictionary.TryGetValue(key, out valueObj) && valueObj is T)
  22. {
  23. value = (T)valueObj;
  24. return true;
  25. }
  26. value = default(T);
  27. return false;
  28. }
  29. /// <summary>
  30. /// Gets a value from the dictionary with given key. Returns default value if can not find.
  31. /// </summary>
  32. /// <param name="dictionary">Dictionary to check and get</param>
  33. /// <param name="key">Key to find the value</param>
  34. /// <typeparam name="TKey">Type of the key</typeparam>
  35. /// <typeparam name="TValue">Type of the value</typeparam>
  36. /// <returns>Value if found, default if can not found.</returns>
  37. public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
  38. {
  39. TValue obj;
  40. return dictionary.TryGetValue(key, out obj) ? obj : default(TValue);
  41. }
  42. /// <summary>
  43. /// Gets a value from the dictionary with given key. Returns default value if can not find.
  44. /// </summary>
  45. /// <param name="dictionary">Dictionary to check and get</param>
  46. /// <param name="key">Key to find the value</param>
  47. /// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
  48. /// <typeparam name="TKey">Type of the key</typeparam>
  49. /// <typeparam name="TValue">Type of the value</typeparam>
  50. /// <returns>Value if found, default if can not found.</returns>
  51. public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> factory)
  52. {
  53. TValue obj;
  54. if (dictionary.TryGetValue(key, out obj))
  55. {
  56. return obj;
  57. }
  58. return dictionary[key] = factory(key);
  59. }
  60. /// <summary>
  61. /// Gets a value from the dictionary with given key. Returns default value if can not find.
  62. /// </summary>
  63. /// <param name="dictionary">Dictionary to check and get</param>
  64. /// <param name="key">Key to find the value</param>
  65. /// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
  66. /// <typeparam name="TKey">Type of the key</typeparam>
  67. /// <typeparam name="TValue">Type of the value</typeparam>
  68. /// <returns>Value if found, default if can not found.</returns>
  69. public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
  70. {
  71. return dictionary.GetOrAdd(key, k => factory());
  72. }
  73. }
  74. }