示例#1
0
        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)));
        }
示例#2
0
        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);
        }