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 GetDescriptionAsync gives incomplete description

I am trying to explore Roslyn's code completion service. I am able to get the completion list, but when I try to get the description it only returns the type and access modifiers, but not the actual description (e.g. Function used for printing to…
1
vote
1 answer

Cannot Compile C# Script via CodeAnalysis: Which assembly am I missing?

The following program attempts to compile and execute a script of C#. However, I keep getting the output: CS0103: The name 'Queryable' does not exist in the current context CS1061: 'int[]' does not contain a definition for 'AsQueryable' and no…
lucidgold
  • 4,432
  • 5
  • 31
  • 51
1
vote
0 answers

How do I make Roslyn aka MSBuildWorkspace behave the same way Visual Studio/MSBuild do? (aka "ResolveComReferences problem")

I'm trying to make Roslyn/MSBuildWorkspace work on some older .NET Framework projects. Those occasionally have COM references, some of them incorrect. Compiling in Visual Studio works with warnings: 2>------ Rebuild All started: Project:…
Tymur Gubayev
  • 468
  • 4
  • 14
1
vote
0 answers

How do I pass a CSharpScript variable a value or object instance before I run it?

Using CSharpScript, is there a way I can pass a variable a value or object instance before the script is run? it seems like using StateScript will allow me to set a script variable using the following. ScriptState state; state.GetVariable("a").Value…
1
vote
1 answer

Code Analysis CA1307 & CA1310 not working in new .NET 6 projects

I'm using Visual Studio 2022 (17.4.4), and when I create a new empty .NET 6 project, Code Analysis doesn't work. According to MSDN, Roslyn Code Analyzers are automatically enabled for every new .NET 6 project: Starting in Visual Studio 2019 16.8…
LTR
  • 1,226
  • 2
  • 17
  • 39
1
vote
1 answer

C# Roslyn - get dependency type out of INamedTypeSymbol

I am trying to find a good solution to finding out whether a dependency on another class in C# is in composition or agreggation. Is there a better way to do it than like this? private static DependencyType GetDependencyAssociation(this…
1
vote
1 answer

Is an ISymbol from different compilations considered equal in an IIncrementalGenerator

One of the stated benefits benefits of using the IIncrementalGenerator over ISourceGenerator is that different stages of the pipeline can recognize that the results of the current iteration is the same as a previous iteration and use cached…
1
vote
1 answer

Roslyn analyzer not loading in dotnet build

tl/dr; My project-referenced Roslyn analyzer works fine in Visual Studio 2019 but fails to load in dotnet build with the following build warning: CSC : warning CS8032: An instance of analyzer…
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
1
vote
0 answers

How to create an Assembly (CSharpCompilation) from an existing Assembly in Roslyn?

In .NET Framework 4.8, i could use AssemblyBuilder to create a new Assembly based on an existing Assembly, add some further modifications (such as encryption) and save it as a new .dll with AssemblyBuilder.Save(). In .NET 6, they removed the…
Florian
  • 11
  • 2
1
vote
1 answer

Can't load dll from Roslyn in a source generator

I'm doing a C# source generator and I want the developer to influence the output of the generated types based on a class with a specified interface that he will implement. The interface is declared in a project called Core.dll. namespace Core { …
Adam Paquette
  • 1,243
  • 1
  • 14
  • 28
1
vote
1 answer

Should a Source Generator be in its own assembly?

In all the examples and documentation I see, source generators are in their own assembly. Is this required? I have a library that currently uses reflection and compiled lambda expressions. I am going to write a generator that will write the classes…
Josh Close
  • 22,935
  • 13
  • 92
  • 140
1
vote
1 answer

.NET 6 Source Generator => TypeDeclarationSyntax get Members of Base Type

we are generating classes from interfaces using source generators. This works fine for non-extended interfaces. However, we also have interfaces which inherit from another interface and we want to create a class containing properties from both…
1
vote
1 answer

SourceGenerator Get all classes which implement an interface decorated by an attribute

I am trying to create a source generator for some automationg purpose. My target types are some classes which implement unknown interfaces. Those interfaces are decorated with a custom attribute. By searching everywhere, I can only find samples…
Shoaib Shakeel
  • 1,447
  • 18
  • 24
1
vote
0 answers

Getting a MetadataReference from an internal Assembly in Blazor WASM

I'm trying to use Roslyn to generate code at runtime in Blazor WASM, however, I am unable to create the MetadataReferences to the necessary assemblies (namely typeof(object).Assembly) for the compilation to succeed. My current code: var syntaxTree =…
Salvage
  • 448
  • 1
  • 4
  • 13
1
vote
1 answer

What is the best practice between var and string for Roslyn compiler in C#?

I want to understand what is the best way for declaring a string, to use var or string if I already know that I have a string. For example I have the following: string str = "here is some text"; or var str = "here is some text"; Which one is…
Eleni S.
  • 31
  • 3