roslyn-code-analysis

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

Finding all not inheriting C# classes with Roslyn and changing to inheriting from base object (java-like)

旧街凉风 提交于 2019-12-05 04:27:56
问题 I'm working on little Roslyn project that includes changing parse tree and writing changes back to file. I've started with standalone code analyzer and want to build it as a command line app. I've encountered a challenge, though. Working with: Find classes which derive from a specific base class with Roslyn partially, and mostly with: https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis I've created this small project: class Program { static void Main(string[] args) {

How do I get a IMethodSymbol or the syntax node of the method declaration from a InvocationExpressionSyntax node which calls the method?

和自甴很熟 提交于 2019-12-04 20:37:52
In the implementation of my analyzer I am in the AnalyzeSymbol method: public override void Initialize(AnalysisContext context) { context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method); } private static void AnalyzeSymbol(SymbolAnalysisContext context) { // here I look through the syntax of the method body in order to find invocations. var methodSymbol = context.Symbol as IMethodSymbol; var location = methodSymbol.Locations.FirstOrDefault(); var methodNode = location.SourceTree.GetCompilationUnitRoot().FindNode(locati‌​on.SourceSpan); var blockNode = methodNode.ChildNodes()

Find all method calls for a specific method using Roslyn

♀尐吖头ヾ 提交于 2019-12-04 13:41:55
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 methodDeclaration = NodeToAnalyze as MethodDeclarationSyntax; if (methodDeclaration == null) return;

Get dependencies between classes in Roslyn

混江龙づ霸主 提交于 2019-12-04 09:55:33
问题 I am successfully getting dependencies between projects with Roslyn, and now I would like to get dependencies between classes, similar to the Code Map feature in Visual Studio Enterprise. Here is my code, the "?????" part is where I imagine I could get something. I am very new to the Roslyn API, though, and I don't know how to proceed from there on. Solution solution = MSBuildWorkspace.Create() .OpenSolutionAsync(Path.Combine(repoRootFolder, "MySolution.sln")) .Result; ProjectDependencyGraph

Using Roslyn, how to enumerate members (Namespaces, classes etc) details in Visual Basic Document?

旧街凉风 提交于 2019-12-04 05:56:52
问题 Using Roslyn, the only mechanism for determining members of Visual Basic document appears to be: var members = SyntaxTree.GetRoot().DescendantNodes().Where(node => node is ClassStatementSyntax || node is FunctionAggregationSyntax || node is IncompleteMemberSyntax || node is MethodBaseSyntax || node is ModuleStatementSyntax || node is NamespaceStatementSyntax || node is PropertyStatementSyntax || node is SubNewStatementSyntax ); How do get the member name , StarLineNumber and EndLineNumber of

Finding all not inheriting C# classes with Roslyn and changing to inheriting from base object (java-like)

天大地大妈咪最大 提交于 2019-12-03 21:39:47
I'm working on little Roslyn project that includes changing parse tree and writing changes back to file. I've started with standalone code analyzer and want to build it as a command line app. I've encountered a challenge, though. Working with: Find classes which derive from a specific base class with Roslyn partially, and mostly with: https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis I've created this small project: class Program { static void Main(string[] args) { try { if (args.Length < 1) throw new ArgumentException(); SyntaxTree tree = CSharpSyntaxTree.ParseText

Get dependencies between classes in Roslyn

笑着哭i 提交于 2019-12-03 03:34:51
I am successfully getting dependencies between projects with Roslyn, and now I would like to get dependencies between classes, similar to the Code Map feature in Visual Studio Enterprise. Here is my code, the "?????" part is where I imagine I could get something. I am very new to the Roslyn API, though, and I don't know how to proceed from there on. Solution solution = MSBuildWorkspace.Create() .OpenSolutionAsync(Path.Combine(repoRootFolder, "MySolution.sln")) .Result; ProjectDependencyGraph projdeps = solution.GetProjectDependencyGraph(); Digraph graph = new Digraph(); foreach (ProjectId

Supporting Roslyn Analyzers(.ruleset) in Visual Studio 2017 .NET Core Projects

梦想与她 提交于 2019-12-01 23:29:43
问题 How to add support for Roslyn Analyzers(.ruleset) in Visual Studio 2017 .NET Core Projects? In "project.json" it is configured by using buildOptions : "buildOptions": { "additionalArguments": [ "/ruleset:rules.ruleset" ] }, 回答1: Just edit the .csproj file and add: <PropertyGroup><CodeAnalysisRuleSet>rules.ruleset</CodeAnalysisRuleSet></PropertyGroup> Note: path is relative to a .csproj file. Original post here: https://www.linkedin.com/pulse/supporting-roslyn-analyzers-visual-studio-2017-net

Getting type from a symbol in roslyn

丶灬走出姿态 提交于 2019-11-30 20:11:41
What is the best general purpose way to get a System.Type from Microsoft.CodeAnalysis.ISymbol for different types of symbols ? (e.g. class declarations, variable, properties, etc) I want to be able to do various checks on the type e.g. as checking if the type implements any interface or is cast-able to any interface, just like one can check on System.Type. The problem I am having is that most of the concrete classes used to represent the symbol are internal (see http://source.roslyn.io/ ) and I could not find tye type information in the ISymbol. SourceNamedTypeSymbol LocalSymbol I retrieve the