tags:

views:

37

answers:

2

I have a fairly simple expression parser that uses the Linq.Expression namespace.

Input is something like: (1+1), it finds the left and right constants, and converts the operator char to an ExpressionTypes enum value, creates the appropriate expression, compiles and executes.

I'd really like to be able to do string manipulations, (abc+def) would evaluate to abcdef for instance, however:

System.InvalidOperationException: The binary operator Add is not defined for the types 'System.String' and 'System.String'

How would I go about implementing this myself?

Something like the equavlant to an ExpressionTypes.Concat would be ideal.

+2  A: 

You have to create the MethodCallExpression yourself, which in this case is for the static method string.Concat. (This is what the compiler does itself when you compile such code)

Kirk Woll
A: 

For custom operators it's actually a method call so you need to find the corresponding method and create the expression tree for calling that method. Haven't work much with expression trees so im affraid I can't give you a code sample but I hope this Helps none the less

Rune FS