How to use roslyn c# compiler with visual studio 2

2019-01-16 09:07发布

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.

So I created a simple console application: hello world example.

Well now I'm expect to choise the c# compiler between the vs2015 default one and roslyn..., but I've not found such option. So my first question is: how to select version of c# compiler?

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. ???? 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.

Well at this point I'm confused... So:

What is roslyn exactly? 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? third: If I build a custom version of roslyn How can I compile my source using Vs2015 ? Which know if csc.exe is roslyn? No help or command line print the codename.

Thanks

3条回答
在下西门庆
2楼-- · 2019-01-16 09:41

Roslyn is the default compiler of Visual Studio 2015. So, if you install VS2015 you´re already using Roslyn.

Roslyn is a codename for .NET Compiler Platform, and it provides open-source C# and Visual Basic compilers. The project is available on github.

查看更多
爷、活的狠高调
3楼-- · 2019-01-16 09:44

Roslyn is two things:

  1. An API that lets you see "compiler things" like syntax trees and symbols.
  2. A new csc.exe that is implemented atop #1.

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. But there's no "choice" dialog like you're looking for.

查看更多
Melony?
4楼-- · 2019-01-16 09:54

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. You'll get to take advantage of all the new C# 6 features that are available only within the new compiler.

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.

Roslyn is also a platform that allows you to build programs that can modify, interpret and understand other programs. 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:

  1. Building Code Analyzers that provide errors and warnings within Visual Studio.
  2. Building extensions for Visual Studio that understand source code.
  3. Building other tools that understand or run source code. Example: ScriptCS - Scripting with C# code.

In order to use Roslyn for these purposes, you pull down the Microsoft.CodeAnalysis packages from NuGet. You can use these packages to parse code, analyze syntax trees, analyze symbols or compile code and emit 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.

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.

If you follow those instructions, you'll be able to run the Roslyn project with F5. It will start a new instance of Visual Studio that's using your customized compiler. This is how people outside of Microsoft will contribute features to the compiler from now on. (Previously you couldn't deploy your custom compiler to Visual Studio but they fixed that in Visual Studio Update 1).

查看更多
登录 后发表回答