Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DictionaryExtensions.cs « Generic « Collections « System « UnitTestFramework « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9942406cde4d368777aa6b4461cb3fe46a831ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;

namespace Microsoft.Internal.Collections
{
    public static class DictionaryExtensions
    {
        public static bool ContainsAllKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
        {
            foreach (TKey key in keys)
            {
                if (!dictionary.ContainsKey(key))
                    return false;
            }

            return true;
        }

        public static bool DictionaryEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary1, IDictionary<TKey, TValue> dictionary2)
        {
            if (dictionary1.Keys.Count != dictionary2.Keys.Count)
            {
                return false;
            }

            foreach (KeyValuePair<TKey, TValue> kvp in dictionary1)
            {
                TValue value1 = kvp.Value;
                TValue value2 = default(TValue);
                if (!dictionary2.TryGetValue(kvp.Key, out value2))
                {
                    return false;
                }

                IDictionary<TKey, TValue> nestedDictionary1 = value1 as IDictionary<TKey, TValue>;
                IDictionary<TKey, TValue> nestedDictionary2 = value1 as IDictionary<TKey, TValue>;

                if ((nestedDictionary1 != null) && (nestedDictionary2 != null))
                {
                    if (!nestedDictionary1.DictionaryEquals(nestedDictionary2))
                    {
                        return false;
                    }
                }
                else
                {
                    if (!(value1.Equals(value2)))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }
}