How to use roslyn c# compiler with visual studio 2015? - c#

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

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:
Building Code Analyzers that provide errors and warnings within Visual Studio.
Building extensions for Visual Studio that understand source code.
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).

Roslyn is two things:
An API that lets you see "compiler things" like syntax trees and symbols.
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.

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.

Related

Aspect Oriented Programming with Roslyn

Does roslyn or visual studio 2015 provide API to rewrite IL or "something like that"? Let me explain...
I've read in msdn magazine's article Use Roslyn to Write a Live Code Analyzer, that it is possible to write custom code analyzers distributed via nuget packages, without need for visual studio plugins.
I'm really curious, if roslyn compiler and visual studio 2015 will allow to implement e.g. Code Cotracts such way, that no plugin is required to build it correctly.
Also, I would like to gain productivity and readability of my code with aspects like [NotifyPropertyChanged] data annotation on properties, but again, if it won't build correctly on every machine with visual studio 2015, it's not a good idea.
It is possible to use DispatchProxy from System.Reflection.DispatchProxy package. Unfortunately I haven't found any examples how to use it, but it looks like a replacement for RealProxy class with some differences (examples how to use RealProxy: hear and here).

How can I avoid a full recompile when compiling a C# project with Roslyn?

I am using Roslyn for some code generation in one of my C# projects. Currently Roslyn will take the entire project, compile it from scratch, then spit out the code I want. This takes a full recompile to do, which is a little slow. When Visual Studio compiles a project it doesn't do it from scratch every time, it does something smart and only re-compiles the parts that have changed. Is there some way to do this with Roslyn?
Visual Studio (in the upcoming version) uses Roslyn to compile the code (to IL). For a single project that is not incremental and never was.
I may be wrong as I'm not sure about Roslyn, but you may be able to seperate the Roslyn code into another solution and have a seperate solution for your own code.
After that you can simply build the solution that changes all the time.

Having problems editing a dll in visual studio

I have a file called Asembly-CSharp.dll i want to edit in C#. I decompile it with .NET reflector's FileGenerator (also tried file disassembler and dotPeek) plugin, thus creating a visual studio project, i open it in visual studio, but it fails to build, i didn't change anything, yet i get lots of errors (like unexpected characters etc), the dll refers to lots of other dlls but even if i put all of them in one solution it fails. Why does it fail if i didn't change anything in the source code (.NET framewrok version is the same as it was before decompilation). Any help appreciated.
I tried both visual studio 2012 and 2013 (win7 but i know that shouldn't be the problem)
I'm not professional, forgive me if i made some silly mistake.
Reverse engineering needs a bit of effort. Nothing comes for free. You need to manually fix those errors.
Mostly these errors occur because either Reflector decompiles the code with compiler-generated variable names that are valid in IL but not in C#, or because that particular assembly was written in some other language that supports features that cannot be directly translated to C#.

What impact installing Roslyn will have on my VS Installation

I wrote a few VS Extensions and Roslyn seems to be an interesting tool in such endeavors. I would be really curious to try it.
But
Is there a safe way to install it? Can I install it in an isolated hive? How cleanly does it uninstall?
Correct me if I am wrong but the Roslyn compiler replaces the 'Standard' C# compiler. If it breaks the result will be quiet unpleasant
I think this thread should have answers to your questions. When you install the CTP it replaces the C# and VB language services under the 'Roslyn' rootsuffix inside Visual Studio. The 'Standard' C# and VB compilers (including the command line compilers vbc.exe and csc.exe) are not replaced and your regular code editing experience inside VS is left unchanged.
In other words, the intention is that the CTP shouldn't impact your existing setup - but to be sure you could follow Hank's suggestion and install it on a Virtual PC.
This thread also has some discussion around this.
Disclaimer: I work for Microsoft on the Roslyn team.
Yes, it's safe to install. I installed on VS 2012 and it was working fine, but it causes problem when used with Resharper. You can even uninstall it with Tools->Extensions and Updates.

Change VB project code to C# using Visual Studio 2010

Does anyone know how to Change VB project code to C# using Visual Studio 2010?
Any help appreciated
I don't believe Visual Studio itself provides any capabilities around this, but there are various other options. There are specific converters such as the ones from Tangible. Alternatively, you could build the VB code and then decompile to C# in something like Reflector or dotPeek. (Latest versions of Reflector are not free, but there is still a version 6 release which will not have a time bomb.)
Note that a verbatim translation of the code is likely to end up calling various methods in the Microsoft.VisualBasic assembly - you'll want to do a bit more work to turn it into idiomatic C#.
Directly in Visual Studio you can't do this. However, you can use tools such as this one or this one.
Telerik has a free converter here

Categories

Resources