反射:递归地搜索对象的字符串值,然后报告路径
我正在尝试在复杂对象中寻找值。有谁知道是否有一个辅助类已经可以做到这一点?
希望能够搜索一个对象,同时观察无限循环保护以找到包含“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键是您希望在递归搜索要查找的值时保留当前属性的
Stack
以及访问对象的HashSet
并跳过它们。您还需要小心异常处理,以便中间的异常不会弄乱堆栈。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 aHashSet
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.