简体   繁体   English

如何在Visual Studio 2015中使用roslyn C#编译器?

[英]How to use roslyn c# compiler with visual studio 2015?

I've a bit of confusion about roslyn. 我对罗琳有些困惑。

What I have done: I've installed vs 2015 community edition and download in extensibilty > download compiler platform sdk. 我做了什么:我已经安装了vs 2015社区版,并以可扩展性下载>下载编译器平台sdk。

So I created a simple console application: hello world example. 因此,我创建了一个简单的控制台应用程序:hello world示例。

Well now I'm expect to choise the c# compiler between the vs2015 default one and roslyn..., but I've not found such option. 好了,现在我期望在vs2015默认值和roslyn之间选择c#编译器...,但是我没有找到这样的选项。 So my first question is: how to select version of c# compiler? 所以我的第一个问题是:如何选择C#编译器的版本?

Second I've downloaded master-roslyn and I build, then I found csc.exe, well the odd things is that if I lauch the exe I get c# compiler version 42.42.42.42. 其次,我下载了master-roslyn并进行了构建,然后我找到了csc.exe,奇怪的是,如果我对exe进行隔离,则会得到c#编译器版本42.42.42.42。 ???? ???? Right? 对?

Then I've follow some tutorials, but all purpose me: to load a source from text file or string vars and analyze or change syntax tree, then compile to var. 然后,我遵循了一些教程,但都是为了我:从文本文件或字符串vars加载源,分析或更改语法树,然后编译为var。

Well at this point I'm confused... So: 好了,我很困惑……所以:

What is roslyn exactly? roslyn到底是什么? A meta compiler? 元编译器? This mean that I can change my code at runtime just like Reflection? 这意味着我可以像反射一样在运行时更改代码? Second: how can compile with vs2015 with default csc or choose roslyn? 第二:如何使用默认csc与vs2015进行编译或选择roslyn? third: If I build a custom version of roslyn How can I compile my source using Vs2015 ? 第三:如果我构建了roslyn的自定义版本,如何使用Vs2015编译源代码? Which know if csc.exe is roslyn? 谁知道csc.exe是roslyn? No help or command line print the codename. 没有帮助或命令行打印代号。

Thanks 谢谢

So it looks like you've got a few questions: 因此,您似乎有几个问题:

What is Roslyn? 罗斯林是什么?

Roslyn is the new default compiler inside of Visual Studio 2015. If you're building and running applications within Visual Studio 2015, they're being compiled with the Roslyn compiler. Roslyn是Visual Studio 2015中的新默认编译器。如果要在Visual Studio 2015中构建和运行应用程序,则将使用Roslyn编译器对其进行编译。 You'll get to take advantage of all the new C# 6 features that are available only within the new compiler. 您将利用仅在新编译器中可用的所有新C#6功能。

If you're using VS2015, Roslyn has replaced the old compiler entirely and as far as I know you can't use the old compiler within VS 2015. 如果您使用的是VS2015,Roslyn已完全替换了旧的编译器,据我所知,您无法在VS 2015中使用旧的编译器。

Roslyn is also a platform that allows you to build programs that can modify, interpret and understand other programs. Roslyn还是一个平台,允许您构建可以修改,解释和理解其他程序的程序。 It's not really meant to let you write code that modifies itself (although that's probably possible to a degree). 并不是要让您编写可自我修改的代码(尽管在某种程度上可能)。

The common use cases for Roslyn are: Roslyn的常见用例是:

  1. Building Code Analyzers that provide errors and warnings within Visual Studio. 构建代码分析器 ,可在Visual Studio中提供错误和警告。
  2. Building extensions for Visual Studio that understand source code. 构建了解源代码的Visual Studio扩展。
  3. Building other tools that understand or run source code. 构建其他理解或运行源代码的工具。 Example: ScriptCS - Scripting with C# code. 示例: ScriptCS-使用C#代码编写脚本。

In order to use Roslyn for these purposes, you pull down the Microsoft.CodeAnalysis packages from NuGet. 为了将Roslyn用于这些目的,请从NuGet中下拉Microsoft.CodeAnalysis程序包。 You can use these packages to parse code, analyze syntax trees, analyze symbols or compile code and emit IL. 您可以使用这些软件包来解析代码,分析语法树,分析符号或编译代码并发出IL。

If you're interested in learning more about Roslyn, I've started a series called Learn Roslyn Now that you might be interested in. 如果您有兴趣了解有关Roslyn的更多信息,我已经开始了一个名为Learn Roslyn的系列, 现在您可能会感兴趣。

Can I replace the compiler? 我可以更换编译器吗?

Yes you can, but I'm not convinced this is a great idea outside of testing changes you want to contribute back to Roslyn. 是的,您可以,但是在测试您想回馈给罗斯林的变更之外,我不认为这是个好主意。 You can pull down Roslyn from GitHub and follow these instructions to build and run Roslyn from within Visual Studio. 您可以从GitHub下拉Roslyn,并按照以下说明从Visual Studio中构建和运行Roslyn。

If you follow those instructions, you'll be able to run the Roslyn project with F5. 如果遵循这些说明,则可以使用F5运行Roslyn项目。 It will start a new instance of Visual Studio that's using your customized compiler. 它将启动使用您的自定义编译器的Visual Studio的新实例。 This is how people outside of Microsoft will contribute features to the compiler from now on. 从现在开始,Microsoft以外的人将通过这种方式为编译器提供功能。 (Previously you couldn't deploy your custom compiler to Visual Studio but they fixed that in Visual Studio Update 1). (以前,您无法将自定义编译器部署到Visual Studio,但他们在Visual Studio Update 1中修复了该问题)。

Roslyn is two things: 罗斯林有两件事:

  1. An API that lets you see "compiler things" like syntax trees and symbols. 一个API,可让您查看“编译器”,例如语法树和符号。
  2. A new csc.exe that is implemented atop #1. 在#1之上实现的新csc.exe。

If you want to make changes to the compiler and use that to build, take a look at these instructions if you haven't already. 如果您想对编译器进行更改并使用它进行构建,请查看这些说明(如果尚未使用的话)。 There's a few different ways you can make your own version of csc.exe and then use that to build something. 您可以通过几种不同的方式来制作自己的csc.exe版本,然后使用它来构建某些内容。 But there's no "choice" dialog like you're looking for. 但是,没有您要找的“选择”对话框。

Roslyn is the default compiler of Visual Studio 2015. So, if you install VS2015 you´re already using Roslyn. Roslyn是Visual Studio 2015的默认编译器。因此,如果您安装VS2015,则说明您已经在使用Roslyn。

Roslyn is a codename for .NET Compiler Platform , and it provides open-source C# and Visual Basic compilers. Roslyn是.NET编译器平台的代号,它提供开放源代码的C#和Visual Basic编译器。 The project is available on github . 该项目位于github上

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

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