I've gone through and beat my head against the wall for a while now searched on various phrases and keywords but I cannot find anything close to an answer so i'm hoping someone here can shed some light.
Basically I'm working on diving pretty deep into manipulating, creating, and modifying Expression Trees in C# 4.0
I came across an odd anomaly I cannot make sense of
if I write something like this
Expression<Func<string,string>> InsertAString = (Insert) => "This " + (Insert == "" ? "" : Insert + " ") + "That";
When I get debug and look at the expression tree it looks similar to this
- F (NodeType = Lambda)
- Body (NodeType = Add)
- Left (NodeType = Add)
- Left (NodeType = Constant, Value = "This ")
- Right (NodeType = Conditional)
- IfFalse (NodeType = Add)
- Left (NodeType = Parameter, Name = "Insert")
- Right (NodeType = Constant, Value = " ")
- IfTrue (NodeType = Constant, Value = "")
- Test (NodeType = Equal)
- Left (NodeType = Parameter, Name = "Insert")
- Right (NodeType = Constant, Value = "")
- IfFalse (NodeType = Add)
- Right (NodeType = Constant, Value = "That")
- Left (NodeType = Add)
- Paramerters (Count = 1)
- Parameters[0] (NodeType = Parameter, Name = "Insert")
- Body (NodeType = Add)
I can call
Console.WriteLine(InsertAString.Compile()("Is Something In-between"));
And I get out as I expect
"This is something In-between That"
Now if i try and rebuild that manually using the static methods of the Expression base class I run into an interesting issue. (I have broken down each step into its own Expression for debugging purposes)
ParameterExpression Insert = Expression.Parameter(typeof(object), "Insert");
ConstantExpression This = Expression.Constant("This ");
ConstantExpression That = Expression.Constant("That");
ConstantExpression Space = Expression.Constant(" ");
ConstantExpression NoCharacter = Expression.Constant("");
BinaryExpression InsertPlusSpace = Expression.Add(Insert,Space);
BinaryExpression InsertEqualsNoCharacter = Expression.Equal(Insert,NoCharacter);
ConditionalExpression InsertPlusSpaceOrNothing = Expression.IfThenElse(InsertEqualsNoCharacter,NoCharacter,InsertPlusSpace);
BinaryExpression ThisPlusInsertPlusSpaceOrNothing = Expression.Add(This,InsertPlusSpaceOrNothing);
BinaryExpression ThisPlusInsertPlusSpaceOrNothingPlusThat = Expression.Add(ThisPlusInsertPlusSpaceOrNothing, That);
Lambda Lambda = Expression.Lambda(ThisPlusInsertPlusSpaceOrNothingPlusThat, Middle);
Expression<Func<string,string>> InsertAString = Lambda as Expression<Func<string,string>>
That based on the values of the generated Expression tree above recreate the same basic expression tree as above (at least with the same "Look")
Everything steps through fine until you get to this line
BinaryExpression InsertPlusSpace = Expression.Add(Insert,Space);
The compiler throws an InvalidOperationException was unhandled
The binary operator Add is not defined for 'System.String' and 'System.String'
Now why is this?
Why when I let C# convert a Lambda into an Expression does it obviously use the Add NodeType, and the Types display show it is definitely using System.String yet when i try and do the same manually it will not let the code continue?
As a Final note I've even tried the following:
BinaryExpression InsertPlusSpace = Expression.MakeBinary( ExpressionType.Add,Insert,Space);
Same error.
I'm curious why it seems at least with what i have been able to find so far that string concatenation in expression trees works only if are not trying to build an expression tree manually that adds constants and variables of type System.String.
Thank you all in advance for the responses.