.net 微软 Roslyn 与 CodeDom
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7852926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microsoft Roslyn vs. CodeDom
提问by mellamokb
From a press releaseyesterday on InfoWorld regarding the new Microsoft Roslyn:
从昨天在 InfoWorld 上发布的关于新的Microsoft Roslyn的新闻稿:
The most obvious advantage of this kind of "deconstructed" compiler is that it allows the entire compile-execute process to be invoked from within .Net applications. Hejlsberg demonstrated a C# program that passed a few code snippets to the C# compiler as strings; the compiler returned the resulting IL assembly code as an object, which was then passed to the Common Language Runtime (CLR) for execution. Voilà! With Roslyn, C# gains a dynamic language's ability to generate and invoke code at runtime.
这种“解构”编译器最明显的优点是它允许从 .Net 应用程序内部调用整个编译执行过程。Hejlsberg 演示了一个 C# 程序,该程序将一些代码片段作为字符串传递给 C# 编译器;编译器将生成的 IL 汇编代码作为对象返回,然后将其传递给公共语言运行时 (CLR) 以执行。瞧!通过 Roslyn,C# 获得了动态语言在运行时生成和调用代码的能力。
I've been able to do this since the release of .NET 4 with CSharpCodeProvider.CompileAssemblyFromSourcewhich I in fact use in an ASP.Net project written awhile ago that does exactly that - allows a user to type in code into a textbox, choose assemblies/namespaces to reference, and then execute and display the output from that code on-the-fly for live environment code testing on Windows Azure.
我已经能够做到这一点,因为.NET 4的发布与CSharpCodeProvider.CompileAssemblyFromSource我在书面前一段时间一个ASP.Net项目所做的正是这实际上使用-允许用户在键入代码到一个文本框,选择组件/命名空间引用,然后即时执行和显示该代码的输出,以便在 Windows Azure 上进行实时环境代码测试。
Is CodeDompart of / a precurser to Roslyn? What's the special benefit of Roslyn over CodeDom?
是CodeDom罗斯林的一部分/前身吗?Roslyn 有CodeDom什么特别的好处?
回答by Dustin Campbell
Disclaimer: I work for Microsoft on the Roslyn team.
免责声明:我在 Roslyn 团队为 Microsoft 工作。
CodeDom is a precursor to Roslyn, but is only marginally related. Essentially, CodeDom is a simple and (somewhat) langage agnostic way to generate code that was added in .NET 1.0 to support designers (a la WinForms). Because CodeDom was an attempt at providing a unified model that can generate code in C#, VB, and other languages, it lacks high fidelity with any of the languages that it supports (that's why you can't create a switch statement with CodeDom). CSharpCodeProvider.CompileAssemblyFromSource is simply a wrapper around executing csc.exe.
CodeDom 是 Roslyn 的前身,但只是略微相关。本质上,CodeDom 是一种简单且(在某种程度上)与语言无关的生成代码的方式,这些代码已添加到 .NET 1.0 中以支持设计器(类似于 WinForms)。因为 CodeDom 试图提供可以生成 C#、VB 和其他语言的代码的统一模型,所以它对它支持的任何语言都缺乏高保真度(这就是您无法使用 CodeDom 创建 switch 语句的原因)。CSharpCodeProvider.CompileAssemblyFromSource 只是执行 csc.exe 的包装器。
Roslyn is a completely different animal. It is a rewrite of both the C# and VB compilers from the ground up using managed code -- C# in C# and VB in VB (the versions of csc.exe and vbc.exe that ship today are written in native code). The advantage of building them in managed code is that users can reference the real compilers as libraries from .NET applications (no wrappers needed).
罗斯林是一种完全不同的动物。它是使用托管代码从头开始重写 C# 和 VB 编译器——C# 中的 C# 和 VB 中的 VB(今天发布的 csc.exe 和 vbc.exe 版本是用本机代码编写的)。在托管代码中构建它们的优点是用户可以将真正的编译器作为来自 .NET 应用程序的库(不需要包装器)来引用。
While building each component of the compiler pipeline, we've exposed public APIs on top:
在构建编译器管道的每个组件时,我们在顶部公开了公共 API:
- Parser -> Syntax Tree API
- Symbol Table/Metadata Import -> Symbol API
- Binder -> Binding and Flow Analysis APIs
- IL Emitter -> Emit API
- 解析器 -> 语法树 API
- 符号表/元数据导入 -> 符号 API
- Binder -> 绑定和流分析 API
- IL 发射器 -> 发射 API
Roslyn can be used as a sophisticated C# and VB source code generator, but that's where the similarity to CodeDom ends. The Roslyn Compiler APIs can be used to parse code, perform semantic analysis, compile and evaluate code dynamically, etc.
Roslyn 可以用作复杂的 C# 和 VB 源代码生成器,但这就是与 CodeDom 的相似之处。Roslyn Compiler API 可用于解析代码、执行语义分析、动态编译和评估代码等。
In addition to the compilers, the Roslyn team is also rebuilding the Visual Studio C# and VB IDE features on topof the public compiler APIs. So, the compiler APIs are rich enough to build the Visual Studio design-time tools, like IntelliSense and the Extract Method refactoring. Also, at layers above the compiler, Roslyn offers services for higher-level analysis or data transformation. For example, there are services for formatting code using the C# and VB formatting rules, or finding all references to a particular symbol within a solution.
除了编译器之外,Roslyn 团队还在公共编译器 API之上重建 Visual Studio C# 和 VB IDE 功能。因此,编译器 API 足够丰富,可以构建 Visual Studio 设计时工具,例如 IntelliSense 和提取方法重构。此外,在编译器之上的层,Roslyn 为更高级别的分析或数据转换提供服务。例如,有一些服务可以使用 C# 和 VB 格式规则来格式化代码,或者在解决方案中查找对特定符号的所有引用。
Really, there isn't just onespecial benefit of Roslyn over CodeDom. Where CodeDom filled a very specific code generation need, Roslyn is tackling the entire language tooling space by providing a framework to allow you to build just about any sort of C# or VB language tool you can think of.
确实,Roslyn 对 CodeDom 的特殊好处不止一个。CodeDom 满足了非常具体的代码生成需求,而 Roslyn 则通过提供一个框架来解决整个语言工具领域,让您可以构建您能想到的几乎任何类型的 C# 或 VB 语言工具。
回答by Reed Copsey
CodeDom allows you to compile - but it doesn't give you the ability to really get information about the code itself (other than compiler errors). Basically, it's a black box where you say "compile this" and it says "I succeeded" or "I failed, here are some errors".
CodeDom 允许您进行编译 - 但它并不能让您真正获得有关代码本身的信息(编译器错误除外)。基本上,它是一个黑匣子,你说“编译这个”,它说“我成功了”或“我失败了,这里有一些错误”。
Roslyn allows you to completely inspect and build out the code on the fly. This includes things like being able to see/inspect the comments within a piece of source code, detailed information about the full structure, etc. You can go through and get the entire syntax tree of the source you pass into Roslyn, and do detailed analysis or transformations on it.
Roslyn 允许您完全检查和动态构建代码。这包括能够查看/检查一段源代码中的注释、有关完整结构的详细信息等。您可以浏览并获取传递给 Roslyn 的源的整个语法树,并进行详细分析或对其进行转换。
Given the full, rich syntax information, you have a huge amount of extra control and flexibility. This is how, for example, the sample works that copies a block of C# code and pastes it as VB.NET code. With Roslyn, you can do more than just compile - you can also manipulate the code itself cleanly. This should make a lot of tooling far simpler to generate, since things like refactorings can be done very simply as the tooling understands the full syntax, including meta information (like comments), and can just work with it directly.
鉴于完整、丰富的语法信息,您将拥有大量额外的控制和灵活性。例如,这就是示例复制一段 C# 代码并将其粘贴为 VB.NET 代码的工作方式。使用 Roslyn,您可以做的不仅仅是编译 - 您还可以干净地操作代码本身。这应该会使很多工具的生成变得更简单,因为重构之类的事情可以非常简单地完成,因为工具理解完整的语法,包括元信息(如注释),并且可以直接使用它。
回答by Simon Mourier
One big difference I see: with CodeDom, each time you compile some C# or VB.NET, it happens out of process. CSC.exe or VBC.exe are the real workers behind the scene.
我看到了一个很大的不同:使用 CodeDom,每次编译一些 C# 或 VB.NET 时,它都会发生在进程之外。CSC.exe 或 VBC.exe 是幕后真正的工作人员。
If you want to build a service, in terms of architecture, scalability, isolation, etc. (you mention Azure), this is not very good.
如果你要构建一个服务,在架构、可扩展性、隔离性等方面(你提到Azure),这不是很好。
With Roslyn it's in process.
Roslyn 正在进行中。
I suppose this is one of the reason they call it "Compiler as a service".
我想这是他们将其称为“编译器即服务”的原因之一。
Also, CodeDom is a relatively poor API, misses a lot of features, and is not really up to date, as it was designed mostly to support Visual Studio UI designers automatic code generation. I think Roslyn will do much better as it's written by the guys who write the compilers. I hope that will make the difference.
此外,CodeDom 是一个相对较差的 API,缺少很多功能,而且不是最新的,因为它主要是为了支持 Visual Studio UI 设计人员自动生成代码而设计的。我认为 Roslyn 会做得更好,因为它是由编写编译器的人编写的。我希望这会有所作为。
PS: One notable difference from CSC.exe and VBC.exe: Roslyn seems to be pure .NET (and uses CCI).
PS:与 CSC.exe 和 VBC.exe 的一个显着区别:Roslyn 似乎是纯 .NET(并使用CCI)。
回答by Yahia
Roslyn allows much much finer control of the whole process - for example you could analyse the string and even generate additional code (on-the-fly within the compile process based on the analysis), etc.
Roslyn 允许对整个过程进行更精细的控制 - 例如,您可以分析字符串,甚至生成额外的代码(在编译过程中基于分析即时生成)等。
CodeDom is "just using the compiler" while Roslyn is "compiler as a service with full access to (sub-) parts"... with Roslyn you are "inside the compiler" and can see what the code looks like from a compiler perspective allowing you to change things in ways currently not possible.
CodeDom 是“只使用编译器”,而 Roslyn 是“编译器即服务,可以完全访问(子)部分”……有了 Roslyn,您就在“编译器内部”,可以从编译器的角度查看代码的样子允许您以目前不可能的方式改变事物。
For example, you can use Roslyn to extend C# - something very handy and much better than the current state of AOP implementation.
例如,您可以使用 Roslyn 来扩展 C#——这非常方便,而且比 AOP 实现的当前状态要好得多。
For an overview of the current Roslyn state and the different levels of access and control it provides, see http://msdn.microsoft.com/en-us/hh500769
有关当前 Roslyn 状态及其提供的不同级别的访问和控制的概述,请参阅http://msdn.microsoft.com/en-us/hh500769
UPDATE
更新
Microsoft just made a new CTP available with additional features and lots of API changes/additions. For details see here.
微软刚刚推出了一个新的 CTP,具有附加功能和大量 API 更改/添加。有关详细信息,请参见此处。

