I'm writing a kind of a mapping tool. I have a method that looks like this (simplified):
public void RegisterMapping<TTarget, TSource>(string propertyName,
Expression<Func<TSource, object>> memberMap)
The memberMap
is an expression defining how to transform a property from TSource
to TTarget
. For the business logic, I need to extract all references to properties of TSource
from it. For example, from
x => x.Customers.Where(c => c.Orders.Any())
I would like to get Customers
, and from
x => x.FirstName + " " + x.LastName
FirstName
and LastName
(could be as string[], PropertyInfo
is trivial to convert to).
How would I do this? My first approach was to traverse the tree manually, checking the node type and inspecting different properties depending on the node type (e.g. Operand
for unary expressions, Arguments
for a function call) to determine of any of these are a property of TSource
. Then I discovered the expression kind list and I gave up -- even if I support only the most common kinds, it's still lots of work. Then I found the ExpressionVisitor. It looks better, but it's still a lot of work to override the visitor methods and I'd like to know if there's an other option, using perhaps a more specialised framework, before I devote my time to this.
Aucun commentaire:
Enregistrer un commentaire