DictionaryExtensions.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Microsoft.Internal.Collections
  7. {
  8. public static class DictionaryExtensions
  9. {
  10. public static bool ContainsAllKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
  11. {
  12. foreach (TKey key in keys)
  13. {
  14. if (!dictionary.ContainsKey(key))
  15. return false;
  16. }
  17. return true;
  18. }
  19. public static bool DictionaryEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary1, IDictionary<TKey, TValue> dictionary2)
  20. {
  21. if (dictionary1.Keys.Count != dictionary2.Keys.Count)
  22. {
  23. return false;
  24. }
  25. foreach (KeyValuePair<TKey, TValue> kvp in dictionary1)
  26. {
  27. TValue value1 = kvp.Value;
  28. TValue value2 = default(TValue);
  29. if (!dictionary2.TryGetValue(kvp.Key, out value2))
  30. {
  31. return false;
  32. }
  33. IDictionary<TKey, TValue> nestedDictionary1 = value1 as IDictionary<TKey, TValue>;
  34. IDictionary<TKey, TValue> nestedDictionary2 = value1 as IDictionary<TKey, TValue>;
  35. if ((nestedDictionary1 != null) && (nestedDictionary2 != null))
  36. {
  37. if (!nestedDictionary1.DictionaryEquals(nestedDictionary2))
  38. {
  39. return false;
  40. }
  41. }
  42. else
  43. {
  44. if (!(value1.Equals(value2)))
  45. {
  46. return false;
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. }
  53. }