比较2个在C#中的深嵌套词典的对象

发布于 2025-02-01 01:28:14 字数 573 浏览 3 评论 0原文

我有一个深嵌套词典:

var a1 = new Dictionary<string, Dictionary<string, Dictionary<string, Person>>>();
var a2 = new Dictionary<string, Dictionary<string, Dictionary<string, Person>>>();

其中person是一些数据结构,其中包含namefamilyage> age。 ..

public class Person
{
    public string name{ get; set; }
    public string family{ get; set; }
    public int age { get; set; }
}

我想比较 A1和A2,尤其是Person的成员之间。 最好的方法是什么?

I have a deep nested dictionaries:

var a1 = new Dictionary<string, Dictionary<string, Dictionary<string, Person>>>();
var a2 = new Dictionary<string, Dictionary<string, Dictionary<string, Person>>>();

where Person is some data structure containing members like name, family, age ...

public class Person
{
    public string name{ get; set; }
    public string family{ get; set; }
    public int age { get; set; }
}

I want to compare between
a1 and a2 and especially between the Person's members.
What is the best way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2025-02-08 01:28:14

我可以认为,我可以将其视为对象的深度比较,而不是仅仅因为以通用方式来涵盖大多数用例的通用方式而不是一项琐碎的任务。对于您而言,特定的一个非常容易实现。您只需要在该比较字典上编写2种方法,而一个比较对象的字典:

void Compare<T, G>(Dictionary<T, G> first, Dictionary<T, G> second)
{
    foreach (var item in first)
    {
        if (!second.ContainsKey(item.Key)) Assert.Fail();

        var secondValue = second[item.Key];

        Compare(item.Value, secondValue);
    }
}

void Compare<T>(T first, T second)
{
    if (first is Dictionary<string, Dictionary<string, Dictionary<string, Person>>> f)
    {
        Compare(f, (Dictionary<string, Dictionary<string, Dictionary<string, Person>>>)(object)second);
    }
    else if (first is Dictionary<string, Dictionary<string, Person>> f1)
    {
        Compare(f1, (Dictionary<string, Dictionary<string, Person>>)(object)second);
    }
    else if (first is Dictionary<string, Person> f2)
    {
        Compare(f2, (Dictionary<string, Person>)(object)second);
    }
    else
    {
        foreach (var property in typeof(T).GetRuntimeProperties())
        {
            Assert.AreEqual(property.GetValue(first), property.GetValue(second));
        }
    }
}

此代码段不涵盖很多内容,但是您可以扩展其以匹配用例。

There is no out of the box solution that I can think of as deep comparison of objects is not given by libraries just because it is not a trivial task to define in a generic way that would cover most use cases. For you specific one it is fairly easy to implement. You just need to write 2 methods on that compares dictionaries and one that compares the objects:

void Compare<T, G>(Dictionary<T, G> first, Dictionary<T, G> second)
{
    foreach (var item in first)
    {
        if (!second.ContainsKey(item.Key)) Assert.Fail();

        var secondValue = second[item.Key];

        Compare(item.Value, secondValue);
    }
}

void Compare<T>(T first, T second)
{
    if (first is Dictionary<string, Dictionary<string, Dictionary<string, Person>>> f)
    {
        Compare(f, (Dictionary<string, Dictionary<string, Dictionary<string, Person>>>)(object)second);
    }
    else if (first is Dictionary<string, Dictionary<string, Person>> f1)
    {
        Compare(f1, (Dictionary<string, Dictionary<string, Person>>)(object)second);
    }
    else if (first is Dictionary<string, Person> f2)
    {
        Compare(f2, (Dictionary<string, Person>)(object)second);
    }
    else
    {
        foreach (var property in typeof(T).GetRuntimeProperties())
        {
            Assert.AreEqual(property.GetValue(first), property.GetValue(second));
        }
    }
}

there is a lot not covered by this code snippet but you can expand of it to match your use cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文