Beispiel #1
0
        private async Task <ITypeSymbol> GetTypeSymbolAsync(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.CreateFromAssembly(typeof(object).Assembly),
                MetadataReference.CreateFromAssembly(typeof(BusinessBase <>).Assembly)
            });

            var model = compilation.GetSemanticModel(tree);
            var root  = await tree.GetRootAsync().ConfigureAwait(false);

            return(model.GetDeclaredSymbol(ITypeSymbolExtensionsTests.FindClassDeclaration(root, name)));
        }
Beispiel #2
0
        private static ClassDeclarationSyntax FindClassDeclaration(SyntaxNode node, string name)
        {
            if (node.Kind() == SyntaxKind.ClassDeclaration)
            {
                var classNode = node as ClassDeclarationSyntax;

                if (classNode.Identifier.ValueText == name)
                {
                    return(classNode);
                }
            }

            foreach (var childNode in node.ChildNodes())
            {
                var childClassNode = ITypeSymbolExtensionsTests.FindClassDeclaration(childNode, name);

                if (childClassNode != null)
                {
                    return(childClassNode);
                }
            }

            return(null);
        }