Just taking a quick break from proof-reading to post a thought I had yesterday. Visual LINQ uses
ToString() to convert an expression tree’s body into readable text. In some cases it works brilliantly, reproducing the original source code exactly – but in other cases it’s far from useful. For instance, from this expression tree representation:
I suspect you wouldn’t have guessed that the original code was:
Personally I don’t find it terrifically obvious, even though I can see how it works. Here’s a thought though – suppose the
LambdaExpression class had a
Source property which the compiler could optionally provide as a constructor argument, so that you could get at the original source code if you needed it. The use in Visual LINQ is obvious, but would it be useful elsewhere?
Well, suppose that LINQ to SQL couldn’t translate a particular query expression into SQL. Wouldn’t it be nice if it could report the part of the query expression it was having trouble with in the language it was originally written in? So a VB expression would give source in VB, a C# expression would give source in C# etc.
All of this would have to be optional, and I suspect some people would have intellectual property concerns about their source leaking out (most of which would be silly, due to the logic effectively being available with a call to
ToString() anyway). I think it would be quite handy in a few situations though.