4

Considering this question of SO, where whole C# in-memory compiler is being called. When only lexical and syntactic analyzing is required: parse text as a stream of lexemes, check them and exit.

Is it possible in current version of System.CodeDom.Compiler, if not - will it be?

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Not to resurrect a dead question, but if you're still trying to do this, check out the Roslyn CTP. It's pretty awesome. – piebie Dec 06 '11 at 21:40

2 Answers2

6

If you can use Mono, I believe it has a C# parser/lexer you may be able to use.

Here's a link to look into. As for what the MS C# team is planning to do, there is some talk of at some point making the C# compiler into a "service" - but it's unclear what that means or when that will happen.

Community
  • 1
  • 1
LBushkin
  • 129,300
  • 32
  • 216
  • 265
1

While it might look like the code is compiled in-memory (CompilerParameters.GenerateInMemory), that's not what actually happens. The same compiler as the one used in Visual Studio is used to compile the code (csc.exe). It gets started by CreateProcess (much like Process.Start) and runs out-of-process to compile the code to an assembly on disk in a temporary folder. The GenerateInMemory option invokes Assembly.LoadFrom() to load the assembly.

You'll get the equivalent of a syntax check simply by setting GenerateInMemory to false and delete the OutputAssembly after it is done.

While this might sound kinda backwards, the huge benefit it has is that this won't put any memory pressure on your process. This will hold you over until C# 5.0 ships.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • So `GenerateInMemory=false` is faster because it doesn't load assembly to memory after successful build? – abatishchev Apr 09 '10 at 22:07
  • That's correct. I doubt you'd notice though. You really don't want to load the assembly anyway, can't unload it. – Hans Passant Apr 09 '10 at 22:44
  • Thanks a lot! I have a project using C# CodeDom, you gave very useful tip. First I thought that the process goes quite the contrary: assembly is being generated in memory and saved on disk – abatishchev Apr 11 '10 at 10:10