c# – 模糊深度对象比较

我有一个方法返回一个包含很多字段的巨大对象.像这样的东西:

{
    Success: true,
    Timestamp: "07.03.2014",
    Items:
    [
        {
            Name: "A",
            Price: 13.37,
            OtherData: 123
        },
        {
            Name: "B",
            Price: 42,
            OtherData: 312
        }
    ]
}

我想将其返回值与.NET应用程序中的测试的参考值进行比较.但是,这里有一些问题:

> Timestamp字段每次都会更改
>价格字段的四舍五入也可能有所不同
>项目的顺序并不重要

我想以最灵活的方式定义一个引用对象:

>省略不必要的字段
>不仅可以指定值,还可以指定规则(字符串的正则表达式,数字的范围/舍入等)

以下是参考定义应如何显示的示例:

{
    Success: true,
    Items:
    [
        {
            Name: "A",
            Price: "13.37",
        },
        {
            Name: "B",
            Price: 42,
        }
    ]
}

是否有任何类型的.NET库允许这样的比较?

最佳答案 最后我决定自己实施比较.这是代码,也许对某人有用:

public static class Utils
{
    public static bool SequencesMatch<TSource, TPattern>(IEnumerable<TSource> sequence, IEnumerable<TPattern> patterns, Func<TSource, TPattern, bool> matcher)
    {
        var items = sequence.Select(x => new SequenceItem<TSource>(x)).ToArray();
        var pats = patterns.Select(x => new SequenceItem<TPattern>(x)).ToArray();

        foreach (var item in items)
        {
            foreach (var pat in pats)
            {
                if (pat.Matched) continue;
                if (matcher(item.Value, pat.Value))
                {
                    item.Matched = pat.Matched = true;
                    break;
                }
            }
        }

        return items.All(x => x.Matched) && pats.All(x => x.Matched);
    }

    public static bool JsonObjectsMatch(JToken data, JToken reference)
    {
        if (reference.Type == JTokenType.Array)
            return SequencesMatch(data, reference, JsonObjectsMatch);

        if (reference.Type == JTokenType.Object)
        {
            var dataObj = data as JObject;
            var refObj = reference as JObject;

            if (dataObj == null || refObj == null)
                Assert.Fail("DataObject = '{0}', ReferenceObject = '{1}'", dataObj, refObj);

            foreach (var pty in refObj)
            {
                var dataValue = dataObj[pty.Key];
                if (dataValue == null || !JsonObjectsMatch(dataValue, pty.Value))
                    Assert.Fail("Objects differ at {0}: DataValue = '{1}', RefValue = '{2}'", pty.Key, dataValue, pty.Value);
            }

            return true;
        }

        if (reference.Type == JTokenType.Float)
        {
            var refFloat = reference.ToObject<float>();
            var dataFloat = data.ToObject<float>();

            if(Math.Abs(dataFloat - refFloat) > 0.001)
                Assert.Fail("Objects differ: DataValue = '{0}', RefValue = '{1}'", dataFloat, refFloat);

            return true;
        }

        return JToken.DeepEquals(data, reference);
    }

    private class SequenceItem<T>
    {
        public T Value { get; set; }
        public bool Matched { get; set; }

        public SequenceItem(T value)
        {
            Value = value;
        }
    }
}

我可能应该把它变成一个合适的库,并在我有足够的时间后在GitHub上发布.

点赞