罗斯林,如何获得所有课程

本文关键字:何获得 罗斯林 | 更新日期: 2023-09-27 18:00:28

如何从Roslyn编译中获取所有类?

var sln = Path.Combine(path, "xxx.sln");
var workspace   = MSBuildWorkspace.Create();
var solution    = await workspace.OpenSolutionAsync(sln);
var project     = solution.Projects.First(x => x.Name == "bbb");
var compilation = await project.GetCompilationAsync();

罗斯林,如何获得所有课程

这就是我访问解决方案中所有类的方式。

class ClassVirtualizationVisitor : CSharpSyntaxRewriter
{   
    List<string> classes = new List<String>();
    
    public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
    {
        node = (ClassDeclarationSyntax) base.VisitClassDeclaration(node);
    
        string className = node.Identifier.ValueText;
        classes.Add(className); // save your visited classes
                
        return node;
    }
}

现在使用访问的类:

var classVisitor = new ClassVirtualizationVisitor();
classVisitor.Visit(semanticModel.SyntaxTree.GetRoot());
    
var classes = classVisitor.classes; // list of classes in your solution

这是我的工作代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using Microsoft.CodeAnalysis.MSBuild;
    using System.Threading.Tasks;
    namespace Kardex.LC3xx.CreateApiDokumentation
    {
        //Patch to work with VS2013
        //  https://support.microsoft.com/en-us/kb/2971005
        class Program
        {
            private static void Main(string[] args)
            {
                Run(args).Wait();
                Console.ReadLine();
            }
            private async static Task Run(string[] args)
            {
                var path = Path.GetDirectoryName(typeof (Program).Assembly.Location);
                var sln = Path.Combine(path, "xxx.sln");
                var workspace = MSBuildWorkspace.Create();
                var solution = await workspace.OpenSolutionAsync(sln);

                Project project = solution.Projects.First(x => x.Name == "bbbb");
                var compilation = await project.GetCompilationAsync();
                foreach (var @class in compilation.GlobalNamespace.GetNamespaceMembers().SelectMany(x=>x.GetMembers()))
                {
                    Console.WriteLine(@class.Name);
                    Console.WriteLine(@class.ContainingNamespace.Name);
                }
                var classVisitor = new ClassVirtualizationVisitor();
                foreach (var syntaxTree in compilation.SyntaxTrees)
                {
                    classVisitor.Visit(syntaxTree.GetRoot());                
                }
                var classes = classVisitor.Classes;             
            }
            class ClassVirtualizationVisitor : CSharpSyntaxRewriter
            {
                public ClassVirtualizationVisitor()
                {
                    Classes = new List<ClassDeclarationSyntax>();
                }
                public List<ClassDeclarationSyntax> Classes { get; set; }
                public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
                {
                    node = (ClassDeclarationSyntax)base.VisitClassDeclaration(node);
                    Classes.Add(node); // save your visited classes
                    return node;
                }
            }
        }
    }

创建一个SymbolVisitor,它覆盖VisitNamedType来处理每个类型(可能不是类)。

然后,将其传递给compilation.Assembly.Accept()