示例#1
0
        /// <summary>
        /// Compares two dictionaries for equality. Each value is compared with equality using Equals
        /// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
        /// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
        /// </summary>
        public static bool Equals <TKey, TValue>(IDictionary <TKey, TValue> left, IDictionary <TKey, TValue> right)
        {
            if (left.Count != right.Count)
            {
                return(false);
            }
            foreach (KeyValuePair <TKey, TValue> leftEntry in left)
            {
                TValue rightValue;
                if (!right.TryGetValue(leftEntry.Key, out rightValue))
                {
                    return(false);
                }

                IEnumerable leftEnumerable  = leftEntry.Value as IEnumerable;
                IEnumerable rightEnumerable = rightValue as IEnumerable;
                if (leftEnumerable == null || rightEnumerable == null)
                {
                    if (!Equals(leftEntry.Value, rightValue))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!Enumerables.Equals(leftEnumerable, rightEnumerable))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
示例#2
0
 public static bool Equals <TKey, TValue>(IDictionary <TKey, TValue> left, IDictionary <TKey, TValue> right)
 {
     if (left.Count != right.Count)
     {
         return(false);
     }
     using (IEnumerator <KeyValuePair <TKey, TValue> > enumerator = left.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             KeyValuePair <TKey, TValue> current = enumerator.Current;
             TValue tValue;
             if (!right.TryGetValue(current.Key, out tValue))
             {
                 bool result = false;
                 return(result);
             }
             IEnumerable enumerable  = current.Value as IEnumerable;
             IEnumerable enumerable2 = tValue as IEnumerable;
             if (enumerable == null || enumerable2 == null)
             {
                 if (!object.Equals(current.Value, tValue))
                 {
                     bool result = false;
                     return(result);
                 }
             }
             else if (!Enumerables.Equals(enumerable, enumerable2))
             {
                 bool result = false;
                 return(result);
             }
         }
     }
     return(true);
 }