roslyn

How to get method definition using Roslyn?

房东的猫 提交于 2019-12-05 12:33:38
How to get method declaration alone from MemberDeclarationSyntax object? How to replace single line and multiline comments from a method definition with empty. Can we do this with SyntaxTriviaList. Here i didn't assigned any object to SyntaxTriviaList. Do we have any method for getting trivia info from definition of body. How to get Method Name alone. private string GetMethodsInSourceFile(string fileName) { SyntaxTree tree = SyntaxTree.ParseFile(fileName); var root = (CompilationUnitSyntax)tree.GetRoot(); IEnumerable<Roslyn.Compilers.CSharp.SyntaxNode> syntaxNodes; syntaxNodes = from

Visual Studio 2017 not highlighting errors

元气小坏坏 提交于 2019-12-05 12:23:18
I recently upgraded from Visual Studio 2012 to 2017 (updated to version 15.3.5) and have some issues with the IDE. In a certain project, I don't get error underlining. and in the Error List pane, "Build Only" actually shows more information than "Build + IntelliSense" Also, with "Build + IntelliSense" selected, I can only see the compiler error in the Output window. Here is the error list after a failed build but the output window has this: 3>------ Build started: Project: ..., Configuration: Debug Any CPU ------ 3>C:\Checkout...\MainForm.vb(454,27): error BC30311: Value of type 'Module1

How to turn off Visual Studio 2015 Roslyn error highlighting?

一曲冷凌霜 提交于 2019-12-05 11:57:24
For Resharper we can selectively turn it on/off, but I could not find a way to turn off the built-in Visual Studio 2015 code highlights/suggestions. The reason I want to do this is because I have quite a few left-join linqs and these queries messed up Visual Studio. I am seeing lots of squiggles on my linqs and I prefer to turn them off. Here is one of the error I am talking about.. I have no idea how to get rid of it. The result of the expression is always 'true' since a value of type 'int' is never equal to 'null' of type 'int?' EDIT I have found a way to fix this from ScottGu's blog here:

Is it possible to use Rosyln or Resharper to detect possible DivideByZero cases?

核能气质少年 提交于 2019-12-05 11:57:05
I'm trying to determine if there's a programmatic way to check for possible DivideByZeroException in my codebase. My codebase includes a series of relatively simple to relatively complex formulas, approximately 1500 of them (and growing). When new formulas are written, care must be taken to ensure that division is done safely to avoid exceptions during processing of these formulas. E.g. decimal val1 = 1.1m; decimal val2 = 0m; var res = val1/val2; //bad var res = val2 == 0 ? 0 : val1/val2; //good Is there any way to use Roslyn or Resharper, or other tools to inspect my codebase and identify

Detect c# 6 features with Roslyn

允我心安 提交于 2019-12-05 11:16:53
Is there a way to detect a c# 6 feature with a roslyn diagnostic analyzer? I have a solution that links in some files from a project that cannot use c#6 features, so I want to make that an error just for those files. To be clear - I cannot set the whole project to c#5, only some files are off limits. I can try and catch specific features, but it's cumbersome and was wondering if there is a faster way? Ivan Kochurkin You can use this Walker for detecting C# 6 syntax features: public class CSharp6FeaturesWalker : CSharpSyntaxWalker { public bool CSharp6Features { get; private set; } public

Checking a Variable Type For Code Analysis

丶灬走出姿态 提交于 2019-12-05 10:56:58
What is the correct way to check a variable's type in a Roslyn code analyzer? I am registering for a ObjectCreationExpressionSyntax node and I can get the type, but I am not certain the correct way to check that it is a type I care about. I found a way to do it by checking the display string, but I am wondering if there is a more correct way to accomplish this. For example, here is code that is trying to check for an ArrayList creation. private static void SyntaxValidator(SyntaxNodeAnalysisContext context) { var creation = (ObjectCreationExpressionSyntax)context.Node; var variableType =

Using the Roslyn Semantic Model to Find Symbols in a Single .cs File

人走茶凉 提交于 2019-12-05 10:54:35
I am using Roslyn to create an analyzer that warns users if a particular class exposes its fields in an unsynchronized manner, to help prevent race conditions. The Problem: I currently have working code that checks to make sure a field is private. I’m having trouble with the last piece of the puzzle: figuring out a way to make sure that all fields are only accessed inside a lock block, so they’re (ostensibly) synchronized. using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft

Where to find Roslyn Project Templates for VS 2015?

限于喜欢 提交于 2019-12-05 10:11:38
I found this question but the accepted answer provide two no working links. If you click on them you will see something like: This item is not yet published. If you are the owner of this project, please sign in with the appropriate account. So my question is. Where to find the installers for Roslyn Project Templates for VS 2015? I believe you're looking for the .Net Compiler Platform SDK: https://visualstudiogallery.msdn.microsoft.com/2ddb7240-5249-4c8c-969e-5d05823bcb89 You may also need to install the Visual Studio 2015 SDK: https://msdn.microsoft.com/en-us/library/bb166441(v=vs.140).aspx

Generating well-formatted syntax with Roslyn

邮差的信 提交于 2019-12-05 10:09:05
I am using Roslyn to modify the syntax of C# files. Using CSharpSyntaxRewriter, it is very easy to find and replace nodes in the syntax tree. However, the generated code is very ugly and won't even parse in all cases because the syntax nodes I create (using SyntaxFactory) lack even a minimal amount of whitespace trivia. Does Roslyn provide some basic formatting functionality to avoid this or do I have to add trivia manually to each node I create? pg0xC Yes - it is possible, using Microsoft.CodeAnalysis.Formatting.Formatter : var formattedResult = Formatter.Format(syntaxNode, workspace); 来源:

Using an AdHocWorkspace results in “The language 'C#' is not supported.”

杀马特。学长 韩版系。学妹 提交于 2019-12-05 10:04:23
Using the RC2 of Microsoft.CodeAnalysis.CSharp.Workspaces in VS2015, this code throws an exception: var tree = CSharpSyntaxTree.ParseText(...); var workspace = new AdhocWorkspace(); var newRoot = Simplifier.Expand(tree.GetRoot(), compilation.GetSemanticModel(tree, false), workspace, n => true, true, CancellationToken.None); The exception message is "The language 'C#' is not supported." What am I missing to make this work? You need to add a reference to the C# Workspaces NuGet package . This will copy the C# DLLs to your output, and let Roslyn's MEF scanner see the language services. 来源: https: