private async Task <IMethodSymbol> GetMethodSymbolAsync(string code, string name) { var(model, root) = await IMethodSymbolExtensionsTests.ParseFileAsync(code); foreach (var method in root.DescendantNodes().OfType <MethodDeclarationSyntax>()) { if (model.GetDeclaredSymbol(method) is IMethodSymbol methodNode && methodNode.Name == name) { return(methodNode); } } return(null); }
private async Task <IMethodSymbol> GetMethodSymbolAsync(string file, string name) { var results = await IMethodSymbolExtensionsTests.ParseFileAsync(file); foreach (var method in results.Item2.DescendantNodes().OfType <MethodDeclarationSyntax>()) { var methodNode = results.Item1.GetDeclaredSymbol(method) as IMethodSymbol; if (methodNode != null && methodNode.Name == name) { return(methodNode); } } return(null); }
private async Task <IMethodSymbol> GetMethodReferenceSymbolAsync(string file, string name) { var results = await IMethodSymbolExtensionsTests.ParseFileAsync( file, MetadataReference.CreateFromFile(typeof(BusinessBase <>).Assembly.Location)); foreach (var invocation in results.Item2.DescendantNodes().OfType <InvocationExpressionSyntax>()) { var methodNode = results.Item1.GetSymbolInfo(invocation); if (methodNode.Symbol != null && methodNode.Symbol.Name == name) { return(methodNode.Symbol as IMethodSymbol); } } return(null); }
private async Task <IMethodSymbol> GetMethodSymbolAsync(string file, string name) { var code = File.ReadAllText(file); var tree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create( Guid.NewGuid().ToString("N"), syntaxTrees: new[] { tree }, references: new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), }); var model = compilation.GetSemanticModel(tree); var root = await tree.GetRootAsync().ConfigureAwait(false); return(model.GetDeclaredSymbol(IMethodSymbolExtensionsTests.FindMethodDeclaration(root, name))); }
private static MethodDeclarationSyntax FindMethodDeclaration(SyntaxNode node, string name) { if (node.Kind() == SyntaxKind.MethodDeclaration) { var methodNode = node as MethodDeclarationSyntax; if (methodNode.Identifier.ValueText == name) { return(methodNode); } } foreach (var childNode in node.ChildNodes()) { var childMethodNode = IMethodSymbolExtensionsTests.FindMethodDeclaration(childNode, name); if (childMethodNode != null) { return(childMethodNode); } } return(null); }
private async Task <IMethodSymbol> GetMethodReferenceSymbolAsync(string code, string name, Type type) { var(model, root) = await IMethodSymbolExtensionsTests.ParseFileAsync( code, MetadataReference.CreateFromFile(type.Assembly.Location)); foreach (var invocation in root.DescendantNodes().OfType <InvocationExpressionSyntax>()) { var symbol = model.GetSymbolInfo(invocation); var methodSymbol = symbol.Symbol as IMethodSymbol; if (methodSymbol == null && symbol.CandidateReason == CandidateReason.OverloadResolutionFailure && symbol.CandidateSymbols.Length > 0) { methodSymbol = symbol.CandidateSymbols[0] as IMethodSymbol; } if (methodSymbol?.Name == name) { return(methodSymbol); } } return(null); }