I know I can walk a linq expression tree and emit the IL code by myself. But i'm trying to avoid that. In this case, using the CodeDom or the Roslyn compiler is not an option (I have to start from an existing set of linq expressions generated by a propietary DSL). Any ideas? Thanks!
Asked
Active
Viewed 1,268 times
3
-
Why can you not walk the LINQ expression tree? – Roy Dictus Dec 09 '13 at 14:49
-
Are you looking for the [Expression
.Compile Method](http://msdn.microsoft.com/en-us/library/bb345362(v=vs.110).aspx)? See also [Can I use reflection to inspect the code in a method?](http://stackoverflow.com/q/2693881/76217) – dtb Dec 09 '13 at 14:51 -
Roy Dictus. I don't want to. Emit IL by hand it's complex and error prone. (I did that in the past and it wasn't easy). I'm looking for somethin similar to assmebly builder but instead of create the method body with opcode use some complied linq. – Ale Miralles Dec 09 '13 at 14:55
-
With regards to Expression
.Compile Method. I'm doing that already. What I want is to save that delegate to disk (as .dll) and then just load the dll and execute it. This is for a very special case when the performance of dinamically compiled linq expression it's a no go. (otherwise works like a charm) Thanks. – Ale Miralles Dec 09 '13 at 14:58 -
@amiralles You need to put an @ in front of the user you wish to notify. – user247702 Dec 09 '13 at 14:59
-
If this was such a great feature then why isn't it already supported? What you are pursuing is the worst possible idea to speed up a program. Google ".net cold start performance" to learn more. – Hans Passant Dec 09 '13 at 15:36
-
@HansPassant Maybe I wasn't clear enough. (I skipped a lot of steps). Right now, the whole process is => source code goes in, (1) we tokenize that stream, (2) we parse it, (3) we generate an ast, (4), then we emit linq expressions based on the ast and (5) finally compile them to a delegate that is ultimately executed. What I wanna do, somehow, is avoid step 1 thru 4 if the source code hasn’t changed. For small/mid sized trees the current process works like a charm, but there are some edge cases (really big trees) that takes for ever to complie. – Ale Miralles Dec 09 '13 at 16:23
1 Answers
3
You can compile an expression to an assembly by using CompileToMethod()
. This has some restrictions, most notably that the MethodBuilder
that you pass to the method has to be for a static method. But otherwise this should be exactly what you're asking for.

svick
- 236,525
- 50
- 385
- 514