roslyn

Opening a solution with msbuildworkspace gives diagnostics errors without details

寵の児 提交于 2019-12-06 02:30:41
I am trying to analyse a solution with Roslyn, with MSBuildWorkspace. The solution is a new solution, with 2 class library projects in them, one referencing the other. They are created in Visual Studio 2017, .Net 4.6.2. When I open the solution, I receive two generic errors in workspace.Diagnostics, both are : Msbuild failed when processing the file ' PathToProject ' There is nothing more in the diagnostics or output window, to indicate WHY it failed to process the project file. The code for opening the solution: namespace RoslynAnalyse { class Program { static void Main(string[] args) {

How to find all the hardcoded values in a C# project(solution)?

你离开我真会死。 提交于 2019-12-06 00:08:14
问题 This question is not asking about hard coded strings only, but magic numbers etc. as well. Is there a way to find all the hard coded values i.e. string , magic numbers and what not in C# project/solution in VS? What prompted this question is a project that I am looking at, I just found 174 times a string value was hardcodely repeated! 回答1: What you could do is program Roslyn, the (not so) new cool kid in town. It allows you to parse C# (or VB.NET) projects quite easily. Then you can visit the

Get semantic model from a classifier VSIX

北战南征 提交于 2019-12-05 22:20:52
As R# is not supporting the Roslyn Early Preview C# 6.0 features, the code looks very dull... I would like to colorize the code using a classifier VSIX. Is it possible to get the semantic model from the Roslyn Language Service for the current document? SLaks You need to add a reference to Microsoft.CodeAnalysis.EditorFeatures.Text.dll, then use the following code. var doc = point.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); var model = await doc.GetSemanticModelAsync(); This requires the Microsoft.CodeAnalysis.EditorFeatures.Text NuGet package (.NET 4.6 required) 来源: https:/

MSBuildWorkspace.Create seems to be not working properly

半腔热情 提交于 2019-12-05 21:45:37
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) Jason Malinowski If I'm reading your comments right, your code is running within Visual Studio, and you're trying to get a workspace to match whatever solution is currently open.

How to find type of the field with specific name in Roslyn

半腔热情 提交于 2019-12-05 19:41:47
Need to find TypeSyntax or essentially Type of a specific filed in class by using Roslyn. Something like this: rootSyntaxNode .DescendantNodes() .OfType<FieldDeclarationSyntax>() .First(x => x.Identifier="fieldName") .GivemeTypeSyntax() But could not get any hint about how to reach Identifier and SyntaxType in FieldDeclarationSyntax node. Any idea please? Part of the issue is that fields can contain multiple variables. You'll look at Declaration for type and Variables for identifiers. I think this is what you're looking for: var tree = CSharpSyntaxTree.ParseText(@" class MyClass { int

Why ScriptEngine has not Execute method anymore?

别来无恙 提交于 2019-12-05 18:38:38
Currently I've started to work with Roslyn more seriously. I found this article about using ScriptEngine on Eric Vogel's blog. Due his blog, there should be an Execute method in this class: scriptEngine.Execute("1+1"); But it seems it doesn't exist anymore. Where does it gone? It was moved to the session object. First you need to var session = engine.CreateSession(SomeHostObject); session.Execute("some code"); Though it should be noted Roslyn has been released as Microsoft.CodeAnalysis now and they "temporarily" removed scripting all together. See from the FAQ 来源: https://stackoverflow.com

Map tokens and trivia to line numbers

穿精又带淫゛_ 提交于 2019-12-05 16:13:54
I'm trying to map tokens and trivia to line numbers using Roslyn. Here's my latest attempt with help from @Kevin Pilch-Bisson below. public class CSharpSlocAnalyser : ISlocAnalyser { public long GetSlocFor(IEnumerable<FileInfo> files, SlocOptions options) { var tree = CSharpSyntaxTree.ParseText( @"using /* Blah */ System; // Blah public class MyClass { public void MyMethod() { var blah = ""abc""; } }"); var root = tree.GetRoot(); var walker = new CustomWalker(); walker.Visit(root); var lineMap = walker.LineMap; return 1; } public class CustomWalker : CSharpSyntaxWalker { public Dictionary<int,

Roslyn VisualBasic.ScriptEngine doesnt recognize hostObject written on C#

人走茶凉 提交于 2019-12-05 14:50:26
Our project need ability to have a simple business rules our customers can script in Visual basic. While our main program is written on C# The script which customers want to execut could be like this (I am considering the simpliest possible case) var vbCode = @" If (Row.Code = 12) Then Row.MappedCode = 1 End If"; So I created a RowData class in C# with Code and MappedCode properties namespace ScriptModel { public class RowData { public int Code { get; set; } public int MappedCode { get; set; } } } I created a simple host object class like namespace ScriptModel { public class HostObjectModel {

Add a parameter to a method with a Roslyn CodeFixProvider

狂风中的少年 提交于 2019-12-05 14:16:56
I'm writing a Roslyn Code Analyzer that I want to identify if an async method does not take a CancellationToken and then suggest a code fix that adds it: //Before Code Fix: public async Task Example(){} //After Code Fix public async Task Example(CancellationToken token){} I've wired up the DiagnosticAnalyzer to correctly report a Diagnostic by inspecting the methodDeclaration.ParameterList.Parameters , but I can't find the Roslyn API for adding a Paramater to the ParameterList inside a CodeFixProvider . This is what I've got so far: private async Task<Document>

Suppressing issues from Roslyn code Analyzers

廉价感情. 提交于 2019-12-05 12:53:11
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. 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 [SuppressMessage] attribute In Visual Studio 2017 you can disable Roslyn warnings (like IDE0002, IDE0003, etc.)