反射:递归地搜索对象的字符串值,然后报告路径

发布于 2024-10-27 06:34:28 字数 181 浏览 2 评论 0原文

我正在尝试在复杂对象中寻找值。有谁知道是否有一个辅助类已经可以做到这一点?

希望能够搜索一个对象,同时观察无限循环保护以找到包含“foo”的 tostring 值,然后返回在其中找到 foo 的属性路径。或者在其中找到 foo 的路径数组。

所以如果在我自己进行此操作之前,有人已经有了方便的现有代码;我将不胜感激。

I am trying to look for a value in a complex object. Does anyone know if there is a helper class out there that does this already?

Would like to be able to search an object, while observing endless loop protection to find a tostring value that contains "foo" and then to return the property path that foo was found in. Or an array of paths foo was found in.

So if someone has handy existing code before I undertake this myself; I would be most grateful.

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

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

发布评论

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

评论(1

无所谓啦 2024-11-03 06:34:28

关键是您希望在递归搜索要查找的值时保留当前属性的 Stack 以及访问对象的 HashSet 并跳过它们。您还需要小心异常处理,以便中间的异常不会弄乱堆栈。

    public static string[] FindPathToProperty(object item, string propertyValueToFind)
    {
        var pathToProperty = new Stack<string>();
        var visitedObjects = new HashSet<object> {item};

        FindPathToProperty(item, propertyValueToFind, pathToProperty, visitedObjects);

        var finalPath = pathToProperty.ToArray();
        Array.Reverse(finalPath);
        return finalPath;
    }

    private static bool FindPathToProperty(object item, string propertyValueToFind, Stack<string> pathToProperty, HashSet<object> visitedObjects)
    {
        foreach (var property in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            try
            {
                var value = property.GetValue(item, null);

                if (visitedObjects.Contains(value))
                {
                    continue;                       
                }

                visitedObjects.Add(value);

                pathToProperty.Push(property.Name);

                bool found = false;
                try
                {
                    found = propertyValueToFind.Equals(value) ||
                            FindPathToProperty(value, propertyValueToFind, pathToProperty, visitedObjects);
                }
                finally
                {
                    if (!found)
                    {
                        pathToProperty.Pop();
                    }
                }
                if (found)
                {
                    return true;
                }
            }
            catch
            {
                continue;
            }
        }

        return false;
    }

    public static void Test()
    {
        Test(new { X = "find" }, "X");
        Test(new { X = "no", Y = "find" }, "Y");
        Test(new { A = new { X = "no", Y = "find" } }, "A.Y");
    }

    private static void Test(object item, string expected)
    {
        string actual = string.Join(".", FindPathToProperty(item, "find"));
        Console.WriteLine("{0} : {1}\r\n      {2}",
                          actual == expected ? "ok " : "BAD",
                          expected,
                          actual);
    }

The key is you want to keep a Stack of the current property while you recursively search for the value you're after and also a HashSet of visited objects and skip them. You also want to be careful with your exception handling so an exception in the middle doesn't mess up the stack.

    public static string[] FindPathToProperty(object item, string propertyValueToFind)
    {
        var pathToProperty = new Stack<string>();
        var visitedObjects = new HashSet<object> {item};

        FindPathToProperty(item, propertyValueToFind, pathToProperty, visitedObjects);

        var finalPath = pathToProperty.ToArray();
        Array.Reverse(finalPath);
        return finalPath;
    }

    private static bool FindPathToProperty(object item, string propertyValueToFind, Stack<string> pathToProperty, HashSet<object> visitedObjects)
    {
        foreach (var property in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            try
            {
                var value = property.GetValue(item, null);

                if (visitedObjects.Contains(value))
                {
                    continue;                       
                }

                visitedObjects.Add(value);

                pathToProperty.Push(property.Name);

                bool found = false;
                try
                {
                    found = propertyValueToFind.Equals(value) ||
                            FindPathToProperty(value, propertyValueToFind, pathToProperty, visitedObjects);
                }
                finally
                {
                    if (!found)
                    {
                        pathToProperty.Pop();
                    }
                }
                if (found)
                {
                    return true;
                }
            }
            catch
            {
                continue;
            }
        }

        return false;
    }

    public static void Test()
    {
        Test(new { X = "find" }, "X");
        Test(new { X = "no", Y = "find" }, "Y");
        Test(new { A = new { X = "no", Y = "find" } }, "A.Y");
    }

    private static void Test(object item, string expected)
    {
        string actual = string.Join(".", FindPathToProperty(item, "find"));
        Console.WriteLine("{0} : {1}\r\n      {2}",
                          actual == expected ? "ok " : "BAD",
                          expected,
                          actual);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文