使用带有反射的 XPath 样式查询 [英] Using an XPath-style Query with Reflection
问题描述
我有一个类树,其中包含层次结构中的多个对象.所以我可能有一个 Container 对象,它托管 3 个 SubContainer 对象,而这些对象又托管任意数量的 Item 对象.
I have a class tree, which contains multiple objects in a hierarchy. So I might have a Container object, which hosts 3 SubContainer objects, which in turn host an arbitrary number of Item objects.
有什么方法可以在这个对象树上使用 XPath 样式的表达式,这意味着反射,以便我可以使用 XPath 样式的表达式查询类的属性,以便代码看起来像一些东西喜欢:
Is there any way that I can use an XPath-style expression over this object tree, which would imply reflection, so that I could query a property of a class with an XPath-style expression, so that the code would look something like:
object o = Container.Query("/Container/SubContainer[1]/Item[1]/@ItemProperty");
显然,这完全是编造出来的,可能不是有效的 XPath 语法,但它只是为了让您了解我在寻找什么.
Obviously that's completely made up and probably isn't valid XPath syntax, but it's just illustratory to give you an idea of what it is I'm looking for.
Edit -> 我想以这种方式查询的原因是因为对象的路径不是固定的,因此需要由应用程序用户进行配置.
Edit -> The reason I would like to query in this manner is because the path to the object is not fixed, and therefore needs to be configurable by the application user.
谢谢.
推荐答案
我遇到了同样的问题,所以写了一个简单的扩展来使用反射查询对象
I had the same kind of problem, so wrote a simple kind of extension to query objects using reflection
http://code.google.com/p/antix-software/wiki/AntixReflectionQuery
如果它不能完全满足您的需求,那至少是一个开始
If it doesn't do exactly what you need, it will at least be a start
这篇关于使用带有反射的 XPath 样式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!