DictionaryExtensions.cs 1017 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MECF.Framework.Common.Extens
  7. {
  8. public static class DictionaryExtensions
  9. {
  10. public static void TryAddValue<TKey, TValue>(this Dictionary<TKey, List<TValue>> dictionary, TKey key, TValue value, bool isReplace = true)
  11. {
  12. if (dictionary.ContainsKey(key))
  13. {
  14. dictionary[key].Add(value);
  15. }
  16. else
  17. {
  18. dictionary.Add(key, new List<TValue> { value });
  19. }
  20. }
  21. public static void TryAddValue<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value, bool isReplace = true)
  22. {
  23. if (dictionary.ContainsKey(key))
  24. {
  25. if (isReplace)
  26. dictionary[key] = value;
  27. }
  28. else
  29. {
  30. dictionary.Add(key, value);
  31. }
  32. }
  33. }
  34. }