roslyn

How do I detect and correct usless try catch blocks? [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 16:37:50
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I've started using the .Net Complier Platform (Roslyn) to assist with enforcing coding standards. One issue I'm struggling with is discovering and catching useless try...catch blocks. For example: // Would like to have this detected and offer to remove the try...catch try { // Do some work } catch

MSBuildWorkspace.Create seems to be not working properly

自古美人都是妖i 提交于 2019-12-07 13:23:41
问题 I am trying to open the solution and want to refer the project.. but solution.Projects always comes empty. Below is my code. Am I missing something? var workspace = MSBuildWorkspace.Create(ImmutableDictionary<string, string>.Empty); var solution = workspace.OpenSolutionAsync(solutionPath).Result; var project = solution.Projects.FirstOrDefault(p => p.Name == projectName); //Here solution.Projects comes empty(count 0) 回答1: If I'm reading your comments right, your code is running within Visual

Generate an enum dynamically from file with intellisense support

*爱你&永不变心* 提交于 2019-12-07 13:16:45
问题 I heard lots about Roslyn, and I just thought is it possible to generate code dynamically from an xml file so that for the developer it will be transparent and he could enumerate the code with IntelliSense as if the code is written in the project. I am writing a framework with a lot of customization done via config files, and would really like to use such feature if it's possible. 回答1: What you want is what F# calls type providers and it probably is possible to do by hacking the Roslyn

.NET Roslyn : runtime configuration

我的梦境 提交于 2019-12-07 12:53:50
问题 I am going to develop some rules with the Roslyn code analyzer. This rule is to control the access of a namespace. Example, the DAL can use only the core. If the View use the DAL, I want a warning. I use the template "Analyzer with Code Fix (NuGet + VSIX)" in 'Visual Studio 2015 Community Edition' to generate the plugin. I have made some test and it works fine. However the rule is written at hard in the code. I don't know how configure the rule in the runtime. The best will be a configuration

How to get all visible local variable names within a scope with Roslyn (Microsoft CodeAnalysis)

◇◆丶佛笑我妖孽 提交于 2019-12-07 11:03:04
问题 (Please note: This is not about run-time reflection/metainfo) I am writing a concrete implementation of Roslyn CSharpSyntaxVisitor When implementing the VisitIdentifierName public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax name) { var symbolInfo = _semanticModel.GetSymbolInfo(name); var fieldSymbol = symbolInfo.Symbol as IFieldSymbol; if (fieldSymbol != null) { // Here I would like to get all the local variable names what are visible // in the very same scope where this

How to get method definition using Roslyn?

旧城冷巷雨未停 提交于 2019-12-07 10:03:50
问题 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

How to get all properties that are anotated with some attribute?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 09:50:36
问题 I am just starting with Roslyn, and I want to find all properties that are annotated with attribute names "OneToOne". I fired up SyntaxVisualizer and was able to get reference to that node, but I am wondering is there a simpler way to achieve this. This is what I have: var prop = document.GetSyntaxRoot() .DescendantNodes() .OfType<PropertyDeclarationSyntax>() .Where(p => p.DescendantNodes() .OfType<AttributeSyntax>() .Any(a => a.DescendantNodes() .OfType<IdentifierNameSyntax>() .Any(i => i

Compile xaml using Roslyn

蹲街弑〆低调 提交于 2019-12-07 08:08:36
问题 Short story: is there a way to use Roslyn compiler to compile a WPF UserControl? Let's say you'd want to compile an assembly containing standard C# code and WPF controls (XAML and xaml.cs files) without any csproj of course. I've found a lot on the web on how to compile a DLL using Roslyn, nothing on how to handle XAML files? I know that Visual Studio (or MSBuild) creates a temporary generated file and embed a BAML file in the resulting assembly. So maybe if I could find a way to generate

Suppressing issues from Roslyn code Analyzers

℡╲_俬逩灬. 提交于 2019-12-07 08:04:33
问题 Is there any way to suppress the issues from the Roslyn Analyzers? I use the instant analyzer project type. And I want to suppress the issues if the user wants it. Also it must be permanent. If I reopen Visual Studio, the same suppression rules must still be applied. 回答1: You can ignore warnings/errors from Roslyn analyzers in exactly the same ways as ignoring normal C# compiler warnings: #pragma disable within source code The Project Properties / Build / Errors and Warnings setting The

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

Deadly 提交于 2019-12-07 06:58:18
问题 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