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
1 answer

Is there Roslyn or FodyWeaver function for replacing inheritance chain to one class?

COM Objects don't allow uses inheritance. I have many classes with devided logic, for re-use code. Can Roslyn change class inheritance and combine classes together? Or any fody weavers? How overwhelmed is using Roslyn for this task? Example: …
1
vote
1 answer

Why does null-forgiving operator (!) does not work for parameter?

Consider the following xUnit Test (this is the most popular and annoying case I have): public class UnitTest1 { [Fact] public async Task Test1() { var result = await this.FindAsync(); Assert.NotNull(result); // Line 1 …
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
1
vote
1 answer

Getting modifiers from a MethodSymbol

How do I get the modifiers from a particular method in a class? I'm able to get the IMethodSymbol but can't find any properties referring to modifiers. I need to know whether the accessibility of the method is greater than private. The class itself…
silkfire
  • 24,585
  • 15
  • 82
  • 105
1
vote
1 answer

How to get type from compilation mode when it is defined in extern alias assembly?

Compilation model has method Compilation.GetTypeByMetadataName with the following description: Gets the type within the compilation's assembly and all referenced assemblies (other than those that can only be referenced via an extern alias) using…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
1
vote
1 answer

VS2017 crashes with 'FileNotFoundEx: System.Runtime.CompilerServices.Unsafe, V=4.0.4.1' upon loading any project

Sorry for a lengthy one, but I'm in dire straits - just trying to provide all details upfront. This Fri (2021-Nov-12) after a restart of Visual Studio 2017 it began crashing without notice while opening existing solutions. This worked perfectly fine…
1
vote
1 answer

Can a VariableDeclarator exist without being a member of a VariableDeclaration in standard C# analysis?

The VariableDeclarator is usually a child of VariableDeclaration. Is this always the case during standard CSharp analysis?
Frank
  • 903
  • 7
  • 14
1
vote
1 answer

How to find out which assembly owns the given ISymbol object obtained from the given `Compilation` object and its declaring syntax references?

I have the following simple unit test: Create base.dll assembly in memory - get its byte array. Create main.dll assembly depending on base.dll in memory - get its byte array. Create CSharpCompilation object from both dlls I will post the complete…
mark
  • 59,016
  • 79
  • 296
  • 580
1
vote
0 answers

How to generate VB.Net code fix in roslyn analyzer

I'm learning how to write Roslyn analyzers. As a toy example I created one which warn if you have assigned a property but not assigned its companion Specified property. (This is how xsd.exe generates classes for XML-schema with optional value…
adrianm
  • 14,468
  • 5
  • 55
  • 102
1
vote
0 answers

Warnings are not treated as errors

Hope you are doing great! I faced a problem with my project where I wanted all analyzers warnings to be errors. I simply can't make all warnings into errors. To reproduce an issue I created a simple project:…
hasrthur
  • 1,410
  • 15
  • 31
1
vote
1 answer

Finding a symbol in a referenced assembly using Roslyn Code Analysis

I need to search the referenced assemblies of the compilation under analysis for types that have the classname specified, irrespective of the namespace it is within. Essentially I need a function that searches the referenced assemblies in the same…
1
vote
1 answer

using roslyn, how to get a type of 'var'?

I want to get the type represented by var. var localDeclaration = methodDeclaration.DescendantNodes().OfType().First(); var identifierName =…
eden
  • 21
  • 2
1
vote
1 answer

Roslyn - OutOfMemoryException due to assemblies loaded in memor

We are doing extensive code analysis on a LOT (thousands) of C# projects. But although we are not calling the Solution.Emit() method, assemblies are still loaded into memory. Are there other methods that also cause this behavior? Is it even possible…
TWT
  • 2,511
  • 1
  • 23
  • 37
1
vote
0 answers

Roslyn throws exception 'Item' is not declared. It may be inaccessible due to its protection level

I'm trying to execute vb script with roslyn script engine my implementation as below in the constructor I initialized all the member Private Sub New(ByVal info As LoggedInUserInfo) _info = info Item = New ScriptItem() …
Gayan
  • 2,750
  • 5
  • 47
  • 88
1
vote
1 answer

Filepath of the assembly being analyzed by C# source generator

How do I find the full filepath of the assembly being analyzed by a C# source generator? I searched everywhere under context.SemanticModel.Compilation but I cannot find this information. I am able to find the sources file location and the full file…
Kilo7
  • 31
  • 3
1
vote
1 answer

Create a FieldExpression instead of ConstantExpression

This is a little bit tricky to explain so I start with an example. class Program { static void Main(string[] args) { var lst = new List(); var value = "John"; var exp1 =…
AliReza Sabouri
  • 4,355
  • 2
  • 25
  • 37
1 2 3
99
100