using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using System.Linq; class MyAnalyzer : DiagnosticAnalyzer { public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration); } private static void AnalyzeMethod(SyntaxNodeAnalysisContext context) { var methodSymbol = context.SemanticModel.GetDeclaredSymbol((MethodDeclarationSyntax)context.Node); var returnType = methodSymbol.ReturnType; // Use the returnType } }
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; class MyAnalyzer : DiagnosticAnalyzer { public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.MethodDeclaration); } private static void AnalyzeMethod(SyntaxNodeAnalysisContext context) { var methodSymbol = context.SemanticModel.GetDeclaredSymbol((MethodDeclarationSyntax)context.Node); if (methodSymbol.IsExtensionMethod) { // Do something } } }This example demonstrates how to use `IMethodSymbol.IsExtensionMethod` property to check whether a method is an extension method. The code uses `SemanticModel.GetDeclaredSymbol` method to get the `IMethodSymbol` instance of the method declaration. The package library is Microsoft.CodeAnalysis.CSharp.Symbol.