gpt4 book ai didi

c# - Roslyn,如何获得所有类(class)

转载 作者:太空狗 更新时间:2023-10-29 18:09:12 25 4
gpt4 key购买 nike

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

        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 == "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

关于c# - Roslyn,如何获得所有类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31143056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com