roslyn-code-analysis

Add module as reference in Roslyn

馋奶兔 提交于 2019-12-11 07:10:45
问题 I'm trying to implement following csc command based compilation with the Roslyn Microsoft.Codeanalysis library csc /target:library /out:UserControlBase.dll UserControlBase.cs /addmodule:"c:\artifacts\MyLib.netmodule" Following is the implementation of the same with Roslyn var compilation = await project.GetCompilationAsync(); //Add Module compilation.AddReferences(ModuleMetadata.CreateFromFile(@"c:\artifacts\MyLib.netmodule").GetReference()); compilationStatus = compilation.Emit

How do you disable Roslyn Analyzers when using msbuild through the command line?

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:05:11
问题 The Roslyn Analyzers are installed as nuget packages, which are dependencies of the FxCop Analyzers (also installed as nuget packages). I have enabled full solution analysis as instructed here: How to Enable and disable full solution analysis for managed code. I have a fairly large solution with most of the projects using the FxCop/Roslyn Analyzers and Visual Studio builds fine, usually in under a minute. However, when running msbuild through the command line using: "C:/Program Files (x86)

Difference between ISymbol.DeclaringSyntaxReferences and ISymbol.Locations

坚强是说给别人听的谎言 提交于 2019-12-10 20:17:40
问题 What is the difference between the DeclaringSyntaxReferences property and Locations property in the ISyntax interface? 回答1: The clue to the answer is in the <remarks> comment section: The syntax node(s) that declared the symbol. If the symbol was declared in metadata or was implicitly declared, returns an empty read-only array. Which means, that Locations also returns metadata reference declarations and implicitly declared locations. You can see evidence for that in the LocationsTests.cs file

What Replaces Code Analysis in Visual Studio 2019?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:50:29
问题 I'm toying with getting our team and projects ready for VS 2019. Right away, trying to set up Code Analysis for a new project, I find this: So, if this is deprecated (and apparently can't even be used, so I'm thinking "deprecated" really means "gone"), where are we supposed to set up our Rule Sets? Is there some other location, or perhaps an altogether new solution to the problem of style and code quality? 回答1: Going forward, static analysis will be provided by Roslyn analyzers: https:/

Create a method call analyzer with Roslyn

纵饮孤独 提交于 2019-12-10 11:53:13
问题 I need to parse a .cs file to look for a particular method. For instance, once the method named "X" is called, the analyzer should detect it. How can detect that this particular node is a method? Thanks in advance! 回答1: If you have a syntax node and semantic model for it you can try this: // node – is your current syntax node // semanticalModel – is your semantical model ISymbol symbol = semanticModel.GetSymbolInfo(node).Symbol ?? semanticModel.GetDeclaredSymbol(node); if(symbol.Kind ==

Using Roslyn, how to check if class comes from a local project, not the BCL or Nuget (etc)?

痞子三分冷 提交于 2019-12-09 03:42:27
I want to write a Roslyn code analyser; which needs to work out if an ObjectCreationExpression is creating an object from a local class (either in the current project or a project in the current solution); or if the class comes from somewhere else, like the Base Class Library or a Nuget package etc. How do I tell where a class comes from in Roslyn? m0sa You can only get that with the help of the semantic model. You can get the symbol for the constructor, and the check where the type comes from via Locations or DeclaringSyntaxReferences , e.g.: // ObjectCreationExpression node == ...; //

Roslyn load project documents failing

 ̄綄美尐妖づ 提交于 2019-12-08 17:28:43
问题 In a Visual Studio Extension (VSIX) solution, I'm using Roslyn to load a specific project from my current solution: Project myProject = this.CurrentComponentModel.GetService<VisualStudioWorkspace>() .CurrentSolution.Projects .FirstOrDefault(p => p.Name == "MyProject") The projct myProject is definitely loaded, but on inspection I see that: myProject.HasDocuments == false myProject.Documents is Empty And yet, in Visual Studio I can see loads of documents. If I close the solution and open the

Using Roslyn, how to check if class comes from a local project, not the BCL or Nuget (etc)?

☆樱花仙子☆ 提交于 2019-12-08 07:02:37
问题 I want to write a Roslyn code analyser; which needs to work out if an ObjectCreationExpression is creating an object from a local class (either in the current project or a project in the current solution); or if the class comes from somewhere else, like the Base Class Library or a Nuget package etc. How do I tell where a class comes from in Roslyn? 回答1: You can only get that with the help of the semantic model. You can get the symbol for the constructor, and the check where the type comes

.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

Find missing await in solution

房东的猫 提交于 2019-12-07 08:23:36
问题 I have lots of async methods in my server code, but I suspect that I have callers without await. Is there a simple way to scan the code for calls where await is missing? public async Task DomeSomethingAsync() { var result = await GetResult(); await StoreResult(result); } then somewhere I've forgot to use await; public async Task SomeBuggyCode() { await Initialize(); DoSomethingAsync(); // DOH - Forgot await } I was hoping there was a smart way to identitfy these erroronous calls. 回答1: This