Questions tagged [roslyn]

Roslyn (aka .NET Compiler Platform) provides open-source C# and Visual Basic compilers with rich code analysis APIs. It enables building code analysis tools with the same APIs that are used by Visual Studio.

Written in 100% managed code, the Roslyn project exposes the C# and VB compilers as a service.

Historically, the managed compilers we’ve shipped in Visual Studio have been opaque boxes: you provide source files, and they churn those files into output assemblies. Developers haven’t been privy to the intermediate knowledge that the compiler itself generates as part of the compilation process, and yet such rich data is incredibly valuable for building the kinds of higher-level services and tools we’ve come to expect in modern day development environments like Visual Studio.

With these compiler rewrites, the Roslyn compilers become services exposed for general consumption, with all of that internal compiler-discovered knowledge made available for developers and their tools to harness. The stages of the compiler for parsing, for doing semantic analysis, for binding, and for IL emitting are all exposed to developers via rich managed APIs.

Useful resources

Tips & Tricks

2826 questions
74
votes
1 answer

How can I make my code diagnostic syntax node action work on closed files?

I'm building a set of code diagnostics using Roslyn (in VS2015 Preview). Ideally, I'd like any errors they produce to act as persistent errors, just as if I were violating a normal language rule. There are a bunch of options, but I'm having a hard…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
73
votes
5 answers

Is it possible to compile a single C# code file with the .NET Core Roslyn compiler?

In old .NET we used to be able to run the csc compiler to compile a single .cs file or several files. With .NET Core we have dotnet build that insists on having a proper project file. Is there a stand-alone command line compiler that would allow to…
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
72
votes
6 answers

Can not delete \bin\roslyn\VBCSCompiler.exe - Access is denied

I am facing a strange issue from roslyn compiler. Sometimes when I build the solution I face a strange issue in error list which does not let me build the solution. Here is the error: Severity Code Description Project File Line …
Rey
  • 3,663
  • 3
  • 32
  • 55
68
votes
1 answer

Enums in lambda expressions are compiled differently; consequence of overload resolution improvements?

While trying out the Visual Studio 2015 RC, I received a run-time error on previously working code. Given the lambda (x => x.CustomerStatusID == CustomerStatuses.Active) which was passed to a function as an Expression<>, the debugger shows a…
Ron Newcomb
  • 2,886
  • 21
  • 24
65
votes
2 answers

Trying to understand ?. (null-conditional) operator in C#

I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { …
Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
64
votes
2 answers

C# 7.3 Enum constraint: Why can't I use the nullable enum?

Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum(this String value, TResult? defaultValue) where TResult : Enum { return String.IsNullOrEmpty(value) ? defaultValue :…
Kirill Kovalenko
  • 2,121
  • 16
  • 18
60
votes
1 answer

Why does this code crash Visual Studio 2015?

For some reason, even so much as typing this into a C# file in Visual Studio is enough to cause it to instantly crash. Why? unsafe struct node { node*[] child; } It seems to occur when the IDE would start coloring keywords and the like. Trying…
Orion
  • 1,157
  • 9
  • 18
60
votes
2 answers

Specifying locale for string interpolation in C#6 (Roslyn CTP6)

String interpolation in C#6 lets me write: decimal m = 42.0m; string x = $"The value is {m}"; However, a very common use case for string formatting is to specify the locale used for formatting the values. Let's say I need to use InvariantCulture…
driis
  • 161,458
  • 45
  • 265
  • 341
59
votes
7 answers

Numerous instances of VBCSCompiler.exe

I just recently downloaded and installed Visual Studio Professional 2015 (14.0.23107.0). The first time I opened up our solution (28 projects) and performed a Build -> Rebuild Solution, my development machine came to an absolute crawl. The CPU was…
John Russell
  • 2,177
  • 4
  • 26
  • 47
56
votes
2 answers

Is Roslyn cross platform?

I've been looking at Roslyn for quite some time now, and I'm curious and excited about it. One thing I noticed is that they mentioned that the compiler is re-written in managed code. This raises the question of whether Roslyn is able to run on…
mirhagk
  • 1,255
  • 3
  • 14
  • 28
55
votes
2 answers

Lowered operations in roslyn

When operations were introduced in Roslyn one of the goals was to provide lowered operations (I think it was in design review meeting video) which as far as I understand should provide explicit operations for implicit compiler actions on high level…
ISanych
  • 21,590
  • 4
  • 32
  • 52
49
votes
3 answers

How to modify code before compilation?

Using Roslyn, I would like to modify my C# code before the actual compilation. For now, I will just need something like: [MyAnotatedMethod] public void MyMethod() { // method-body } And based on the annotation, I would like to inject some…
Lukas Durovsky
  • 559
  • 4
  • 8
48
votes
3 answers

Getting debugger context in C# interactive

C# Interactive seems a lot more powerful than the Immediate Window (at least it handles lambda expressions that are often used in LINQ - see Visual Studio debugging "quick watch" tool and lambda expressions), but it looks like it can't be used as a…
user276648
  • 6,018
  • 6
  • 60
  • 86
48
votes
8 answers

How do I disable all Roslyn Code Analyzers?

I'm trying to work with a large opensource project that has a handful of Roslyn Code Analyzers. When I open the solution Visual Studio uses ~35% CPU for about 15 minutes. Using PerfView I've figured out that the code analyzers being run on the…
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
45
votes
2 answers

Why do we get possible dereference null reference warning, when null reference does not seem to be possible?

Having read this question on HNQ, I went on to read about Nullable Reference Types in C# 8, and made some experiments. I'm very aware that 9 times out of 10, or even more often, when someone says "I found a compiler bug!" this is actually by design,…
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158