roslyn-code-analysis

When to use SemanticModel.GetSymbolInfo and when SemanticModel.GetDeclaredSymbol

℡╲_俬逩灬. 提交于 2020-01-14 07:04:50
问题 In some cases, when I'm trying to get the the ISymbol for my syntax node, I'm fail (getting null) when using SemanticModel.GetSymbolInfo but succeed when using SemanticModel.GetDeclaredSymbol. I've attached an example bellow. So my question is when to use each one of the methods for getting the semantic model? public class Class1 { public System.String MyString { get; set; } public static void Main() { var str = @" namespace ClassLibrary31 { public class Class1 { public System.String MyString

Find all method calls for a specific method using Roslyn

大憨熊 提交于 2020-01-13 05:18:46
问题 I am working on a code analyser using Roslyn and my current task is to find all internal methods which are unused in the assembly. I start with a MethodDeclarationSyntax and get the symbol from that. I then use the FindCallersAsync method in SymbolFinder , but it returns an empty collection even when I am making a call to the method in question somewhere in the assembly. See the code below. protected override void Analyze(SyntaxNodeAnalysisContext context) { NodeToAnalyze = context.Node; var

How can I obtain return type from method passed as an argument in another method with Roslyn?

好久不见. 提交于 2020-01-06 04:50:08
问题 Lets say that I have a code: public class Test { private readonly IFactory _factory; private readonly ISomeClass _someClass; public Test(IFactory factory, ISomeClass someClass) { _factory = factory; _someClass = someClass; } .... public void TestMethod() { _someClass.Do(_factory.CreateSomeObject()); } } public class Factory { public SomeObject CreateSomeObject() { return new SomeObject(); } } public class SomeClass { public void Do(SomeObject obj){ .... } } I would like to get return type of

VSIX: Adding Projects with Roslyn refactoring options says “Not supported”

强颜欢笑 提交于 2020-01-04 09:16:08
问题 I feel like there should be an easy way to do this. I mean it can't be possible that you can't add projects via extensions? What I want to do: I want to write a small extension that adds a "code refactoring" option to methods that will generate a test class and method for it in the matching test project. If the test project does not exist, I will create it. Everything is working so far, except that when I try to use Solution.AddProject() it throws the exception System.NotSupportedException :

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

馋奶兔 提交于 2020-01-02 07:27:30
问题 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? 回答1: Part of the issue is that fields can contain multiple variables. You'll look at Declaration for type and Variables for identifiers. I

Compiler Issue in Visual Basic Project With Microsoft.CodeAnalysis.Emit

大兔子大兔子 提交于 2019-12-25 19:04:27
问题 I have developed following code to generate dll files using Microsoft.CodeAnalysis.Emit library. The code successfully generates dll files for C# projects. However it doesn't successfully build Visual Basic projects. It throws lot of compiler errors for VB projects which build successfully using the VS IDE. Please see the errors thrown for a basic Windows application project. Is there any specific compiler options for VB projects? Please advice how to resolve this. Microsoft.CodeAnalysis

What is the canonical way to convert a MethodDeclarationSyntax to a ConstructorDeclarationSyntax?

为君一笑 提交于 2019-12-25 04:23:00
问题 I'm writing an analyzer and codefix for an API migration (AutoMapper V5 Profiles), converting a protected override Configure method to a constructor: from: public class MappingProfile : Profile { protected override Configure() { CreateMap<Foo, Bar>(); RecognizePrefix("m_"); } } to public class MappingProfile : Profile { public MappingProfile() { CreateMap<Foo, Bar>(); RecognizePrefix("m_"); } } I've found a way to convert the method node into a constructor, but I've been struggling a lot to

Some projects' dependencies contain unexplained code analyzers

萝らか妹 提交于 2019-12-24 17:26:06
问题 I have a couple of projects where under Dependencies there are one or two branches of code analyzers like this: I tried the right mouse click on every branch as explaind here but there is no option to configure them and I also searched for *.ruleset files insinde the solution, I also check the packages as shown in the docs but there is nothing like that anywhere. There is also nothing suspicious in the .csproj files, just some nuget packages that I've installed but nothing analyzer related.

Make MSBuild use new Roslyn compiler in VS15 Preview

梦想与她 提交于 2019-12-24 08:25:00
问题 I just recently delved into Roslyn compiler stuff and I'm wondering about some possibilities. I'm trying to build Roslyn compiler from Roslyn repository using features/tuples branch and replace the needed DLLs in VS15Preview\MSBuild\15.0\Bin folder. But when I replace DLLs and build console app project I receive the following error message Severity Code Description Project File Line Suppression State Error The specified task executable "csc.exe" could not be run. Could not load file or

Calling Roslyn Analyzers from command line

*爱你&永不变心* 提交于 2019-12-23 21:26:49
问题 Using Roslyn Analyzers while developing in Visual Studio 2015 is great. However, it would be even greater to be able to call analyzers from a pre-commit hook or a CI like TeamCity in order to make sure non-conformant code is flagged. Is there any way to get the results of an analyzer by calling a command line utility or will this need to be custom made? 回答1: Roslyn Analyzers can be ran by csc.exe or vbc.exe (the compilers themselves), so just invoke the compiler. Assuming your Continuous