views:

106

answers:

1

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.

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");

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 -> 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.

Thanks.

A: 

You can write an extension method to Control that takes a string as you describe.

You will have to write the parser for it and use that to do the actual querying.

Oded
I know that, although it wouldn't be a Control that I'd want to extend, it would be Object. But what I'm asking is if there is actually something that exists at present, like JXPath for Java (http://commons.apache.org/jxpath/).
Matt Whitfield
@Matt Whitfield - I am not familiar with anything of this type, but someone else may know.
Oded
Thanks for your input! :)
Matt Whitfield
I don't think you will find anything, as Linq makes such queries so much easier.
Oded
I may be missing the point, and if I am, then please show me the error of my ways, but how would Linq help me if the path to the object in question is not fixed and has to be user configurable? (edit -> sorry I didn't specify that in the question, I will edit it now)
Matt Whitfield