roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Semantic search should check project's type

Open Cosifne opened this issue 9 months ago • 0 comments

Using Roslyn.sln, and copy this code to the semantic search window

using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

static IEnumerable<ISymbol> Find(CSharpCompilation compilation)
{
	var asyncMethods = new List<ISymbol>();

	foreach (var syntaxTree in compilation.SyntaxTrees)
	{
		var semanticModel = compilation.GetSemanticModel(syntaxTree);
		var root = syntaxTree.GetRoot();

		var methodDeclarations = root.DescendantNodes().OfType<MethodDeclarationSyntax>();

		foreach (var methodDeclaration in methodDeclarations)
		{
			if (methodDeclaration.Modifiers.Any(SyntaxKind.AsyncKeyword))
			{
				var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration);
				asyncMethods.Add(methodSymbol);
			}
		}
	}

	return asyncMethods;
}

VS shows: image

Cosifne avatar Apr 30 '24 21:04 Cosifne