0

首先,我是泛型的新手,所以我提前为任何错误道歉。

我希望能够以统一的方式比较不同类型的词典。我要比较 2 个可能具有这种类型的字典(为了清楚起见,我想比较 2 个具有相同类型的不同字典):

Dictionary<string, Int32>
Dictionary<Int32, Int32>
Dictionary<string, string>
Dictionary<string, List<Int32>>
Dictionary<Int32, List<Int32>>
Dictionary<Int32, List<ComplexObject>>

使用泛型,我得到了这段代码:

 private static bool DictionaryEquals<TKey, TValue>(Dictionary<TKey, TValue> left, Dictionary<TKey, TValue> right)
    {
        var comp = EqualityComparer<TValue>.Default;
        if (left.Count != right.Count)
        {
            return false;
        }
        foreach (var pair in left)
        {
            TValue value;
            if ((typeof(TValue).Namespace == "System.Collections.Generic"))
            {
                TValue rightValue;
                right.TryGetValue(pair.Key, out rightValue);
                return ListEquals<TValue>(new List<TValue>(pair.Key), rightValue);
            }

            if (right.TryGetValue(pair.Key, out value) || (!comp.Equals(pair.Value, value)))
            {
                return false;
            }
        }
        return true;
    }

    private static bool ListEquals<TValue>(List<TValue> left, List<TValue> right)
    {
        if (left.Count != right.Count)
        {
            return false;
        }

        return left.All(right.Contains);
    }

我在调用 ListEquals 方法时遇到问题,我不知道如何传入 pair.key 和 rightValue 参数。

感谢您的任何建议

4

4 回答 4

0

如果TValue是 rightValue 的类型,则 TValue 是 a List<SomeType>。但是使用 ListEquals,而不是List<SomeValue>

于 2012-11-28T17:35:56.457 回答
0

我更改了您的代码,这是有效的,但我不知道如何替换dynamic,这不是最佳选择,但可以解决所有铸造问题。

        private static bool DictionaryEquals<TKey, TValue>(Dictionary<TKey, TValue> left, Dictionary<TKey, TValue> right)
        {
            var comp = EqualityComparer<TValue>.Default;
            if (left.Count != right.Count)
            {
                return false;
            }

            if (typeof(TValue).IsGenericType && typeof(TValue).GetGenericTypeDefinition() == typeof(List<>))
            {
                return left.All(pair => right.ContainsKey(pair.Key) && ListEquals((dynamic)pair.Value, (dynamic)right[pair.Key]));            
            }
            else
            {
                return left.All(pair => right.ContainsKey(pair.Key) && comp.Equals(pair.Value, right[pair.Key]));
            }
        }

        private static bool ListEquals<TValue>(List<TValue> left, List<TValue> right)
        {
            if (left.Count != right.Count)
            {
                return false;
            }

            return left.All(right.Contains);
        }
于 2012-11-28T17:58:08.663 回答
0

这是我解决问题的方法:

private static bool DictionaryEquals<TKey, TValue>(Dictionary<TKey, TValue> left, Dictionary<TKey, TValue> right)
{
    var comp = EqualityComparer<TValue>.Default;
    if (left.Count != right.Count)
    {
        return false;
    }

    if (left.Keys.Intersect(right.Keys).Count() != left.Count)
        return false;
        //there is a key in the left dictionary that's not in the right dictionary
        //if there are any keys in the right dictionary not in the left then either 
        //there is one in the left not in the right as well, or the counts won't have 
        //been equal, so we know the two key sets are equal.

    var defaultValueComparer = EqualityComparer<TValue>.Default;

    Func<TValue, TValue, bool> valueComparer;

    if (typeof(TValue) is IEnumerable)
        valueComparer = (first, second) => ((IList)first).SequenceEqual((IList)second);
    else
        valueComparer = (first, second) => defaultValueComparer.Equals(first, second);

    foreach (var key in left.Keys)
    {
        if (!valueComparer(left[key], right[key]))
            return false;
    }

    return true;
}

public static bool SequenceEqual(this IList first, IList second)
{
    if (first.Count != second.Count)
        return false;

    IEnumerator iterator1 = first.GetEnumerator(),
                iterator2 = second.GetEnumerator();

    while (true)
    {
        bool next1 = iterator1.MoveNext();
        bool next2 = iterator2.MoveNext();
        // Sequences aren't of same length. We don't 
        // care which way round. 
        if (next1 != next2)
        {
            return false;
        }
        // Both sequences have finished - done 
        if (!next1)
        {
            return true;
        }
        if (!object.Equals(iterator1.Current, iterator2.Current))
        {
            return false;
        }
    }
}

有几点需要注意:

我没有让这两个集合成为“集合比较”,而是让它们成为序列比较。如果他们真的应该有一个集合比较,那么首先将它们作为列表以外的东西会更有意义。无论如何,如果这是一个重要的更改,您可以将我的SequenceEqual方法的名称和实现修改为以下内容:

public static bool SetEquals(this IList first, IList second)
{
    if (first.Count != second.Count)
        return false;

    return first.OfType<object>().Intersect(second.OfType<object>())
        .Count() < first.Count;
}

与其确定如何比较 foreach 内部的值,不如在外部进行比较;值的类型在循环中不会改变。确定比较函数将是一次,然后一遍又一遍地调用它。代表们在这方面做得很好。

于 2012-11-28T18:05:09.043 回答
0

这样的事情不应该吗?

public class DictionaryEqualityComparer<TKey,TValue> : IEqualityComparer<Dictionary<TKey,TValue>>
{
    public bool Equals( Dictionary<TKey , TValue> x , Dictionary<TKey , TValue> y )
    {
        bool unequal =  x.Count != y.Count
                     || x.Except( y ).Any() // this is probably redundant
                     || y.Except( x ).Any() // but my caffiene titration is off this AM
                     ;
        return !unequal ; 
    }
    public int GetHashCode( Dictionary<TKey , TValue> obj )
    {
        return obj.GetHashCode() ;
    }

有几个原因。最大的一个是相同类型的两个字典Dictionary<string,Widget>可以使用不同的相等比较器作为键。在字符串键的实例中,它可能是任何一种库存StringComparer实现。这使“平等”的概念复杂化。

于 2012-11-28T18:31:07.827 回答