C# 使用Roslyn获取公共函数
我有下面的代码,从文件中提取所有方法C# 使用Roslyn获取公共函数,c#,roslyn,C#,Roslyn,我有下面的代码,从文件中提取所有方法 var tree = file.GetSyntaxTreeAsync().Result; var methodDeclarations = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList(); var tree=file.GetSyntaxTreeAsync().Result; var methodDeclarations=tree.GetRoo
var tree = file.GetSyntaxTreeAsync().Result;
var methodDeclarations = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList();
var tree=file.GetSyntaxTreeAsync().Result;
var methodDeclarations=tree.GetRoot().genderantnodes().OfType().ToList();
如何仅获取公共函数?如果要在文件中查找标记为公共的方法,可以使用MethodDeclarationSyntax对象的Modifiers属性,如下所示:
var publicMethods = root.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Where(method =>
method.Modifiers.Where(modifier =>
modifier.Kind() == SyntaxKind.PublicKeyword)
.Any());
var publicMethods=root.genderantnodes()
第()类
。其中(方法=>
方法。修饰符。其中(修饰符=>
修饰符.Kind()==SyntaxKind.PublicKeyword)
.Any());
如果您对该文件中声明的类的所有公共方法感兴趣,则需要听取@Slaks的建议并使用语义模型,因为父类中声明的公共方法不会仅使用语法分析来显示。您需要语义模型。