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
1
vote
0 answers

Roslyn Blazor WASM: Can't Create A Metadata Reference To An Assembly Without Location

I'm trying to evaluate very simple formulas at runtime using Roslyn in a Blazor environment. Here is an example: ScriptOptions = ScriptOptions.Default.AddImports("System"); var formula = "20 + 10"; var res = CSharpScript.EvaluateAsync(formula,…
Aaron Ullal
  • 4,855
  • 8
  • 35
  • 63
1
vote
2 answers

Usings before/after file-scoped namespace

C# 10 gave us file-scoped namespaces. So this: using Foo; using Bar; namespace Baz { // ... } can be written as: using Foo; using Bar; namespace Baz; // ... There are differences between usings before and after a namespace. Does the compiler…
lonix
  • 14,255
  • 23
  • 85
  • 176
1
vote
1 answer

Visual C# Compiler (csc) unable to compile programs with latest language features

For learning purposes, I use csc.exe myprogram.cs shipped with Visual Studio Community (my version 17.3.4) to compile basic C# programs. It mostly works, except when using what appears to be latest language features, for example, array range…
user124
  • 1,632
  • 1
  • 11
  • 11
1
vote
1 answer

Visual Studio error ENC0039: What's the rationale behind it?

I'm debugging an application in VS 2022. I noticed that one of my ||'s wasn't properly formatted, so I fixed it. // Before while ((deq = ItemsToAppend.TryDequeue(out T? item))|| CanAppend) // After while ((deq = ItemsToAppend.TryDequeue(out T?…
Jakob Lovern
  • 1,301
  • 7
  • 24
1
vote
0 answers

Visual Studio errors disappear when open file

I'm in the process of updating a library in a larger solution. For this purpose, I have removed the old references in all projects and added new ones. If I compile the solution now I get about 3000 build errors, which indicate that various classes…
ringhat
  • 23
  • 4
1
vote
0 answers

How to create a console application using Roslyn programmatically?

My goal is to create a console application using Roslyn programmatically: string codeToCompile = @" using System; public class Program { public static void Main() { Console.WriteLine(""Hello World!""); Console.ReadLine(); …
Rafi Henig
  • 5,950
  • 2
  • 16
  • 36
1
vote
0 answers

Why isn't List> implictly converted to IList?

Why can a List not be passed to a function which accepts an IList? I can see how this type conversion may be hard to infer. However, it seems to work fine when converting to an IEnumerable or starting with a…
andypea
  • 1,343
  • 11
  • 22
1
vote
1 answer

Roslyn code fix runs an extra time, unit tests fail - too many iterations

I'm working on a somewhat 'Hello World' type analyzer+code fix with the C# Roslyn API, and running into a test failure which I don't understand and haven't had much luck in tracking down any additional information on thus far. I have a suite of…
Chris Thompson
  • 490
  • 5
  • 17
1
vote
0 answers

Reading a Roslyn compilation from DLL on disk

I realize that this is slightly backwards, but I am trying to read create a Roslyn Compilation object from a previously generated C# NuGet package. I cannot find any obvious way to achieve this. My use case is that I want to compare the syntax trees…
UndyingJellyfish
  • 194
  • 2
  • 15
1
vote
0 answers

How to use Roslyn to determine code's line and column position in a source file

I am using Roslyn within a source generator to find certain c# method calls in a syntax tree. When found, I want to note the line and column number. This answer works for the line number but not the column number. This is how I get the LineSpan for…
1
vote
1 answer

Can I combine multiple IncrementalValuesProvider into an object that makes sense?

I want to combine multiple IncrementalValuesProvider I don't find var result = a.Combine(b.Combine(c)); very appealing, because result.Left.Left and result.Right.Left etc are not intuitive at all. Is there some way I can combine multiple…
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
1
vote
1 answer

Roslyn emit an assembly with AssemblyInfo.cs properties

I'm trying to emit a dll using Roslyn CSharpCompilation on NET 6 with the assembly info (AssemblyInfo.cs) but it's not working. I'm not getting any error but the binary file doesn't contains any property inside. Complete Single file example (copy to…
Santiago
  • 2,190
  • 10
  • 30
  • 59
1
vote
1 answer

Roslyn - how to get property names and types in a class using Roslyn?

I created a VSIX project to read properties of a class, trying to get their names and varriable types as doing sth like below using Roslyn; string filePath = Path.GetDirectoryName(dte.Solution.FullName) + @"\ProjectName\Models\Products.cs"; …
Ali CAKIL
  • 383
  • 5
  • 21
1
vote
1 answer

Cannot read the external namespaces (Microsoft.AspNetCore.MVC...) in Roslyn (in another project it is working)

I have set up one Roslyn analyzer project that is reading properly the (external and internal) namespaces from Symbol (open solution in workspace -> read documents -> read classes -> read class symbol); however, when I created another xUnit project…
ce8
  • 23
  • 1
  • 3
1
vote
1 answer

Roslyn CompletionService does not infer type for implicit foreach variable

I'm using Roslyn's CompletionService for autocomplete functionality in my app, that allows users to write and execute c# scripts. When writing an expression such as (pipe denotes the caret position): var m = Selected.Measure; m.| Roslyn is…
Dan
  • 10,480
  • 23
  • 49