简体   繁体   English

是否可以在没有编译的情况下调用C#词法/句法分析器?

[英]Is it possible to call C# lexical/syntactic analyzers without compilation?

Considering this question of SO, where whole C# in-memory compiler is being called. 考虑到SO的这个问题 ,调用整个C#内存编译器。 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? 是否有可能在当前版本的System.CodeDom.Compiler中 ,如果不是 - 它会是吗?

If you can use Mono, I believe it has a C# parser/lexer you may be able to use. 如果你可以使用Mono,我相信它有一个你可以使用的C#解析器/词法分析器

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. 至于MS C#团队计划做什么, 有一些关于将C#编译器变成“服务”的讨论 - 但目前还不清楚这意味着什么或何时会发生。

While it might look like the code is compiled in-memory (CompilerParameters.GenerateInMemory), that's not what actually happens. 虽然看起来代码是在内存中编译的(CompilerParameters.GenerateInMemory),但实际上并不是这样。 The same compiler as the one used in Visual Studio is used to compile the code (csc.exe). 与Visual Studio中使用的编译器相同的编译器用于编译代码(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. 它由CreateProcess启动(非常类似于Process.Start)并在进程外运行以将代码编译到临时文件夹中的磁盘上的程序集。 The GenerateInMemory option invokes Assembly.LoadFrom() to load the assembly. GenerateInMemory选项调用Assembly.LoadFrom()来加载程序集。

You'll get the equivalent of a syntax check simply by setting GenerateInMemory to false and delete the OutputAssembly after it is done. 只需将GenerateInMemory设置为false,就可以获得等效的语法检查,并在完成后删除OutputAssembly。

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. 这将阻止你直到C#5.0发货。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM