Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在源生成器中查找类声明的文件路径_C#_.net_Sourcegenerators - Fatal编程技术网

C# 如何在源生成器中查找类声明的文件路径

C# 如何在源生成器中查找类声明的文件路径,c#,.net,sourcegenerators,C#,.net,Sourcegenerators,标题很好地解释了这一点。我正在编写一个源代码生成器,想知道如何找到给定ClassDeclarationSyntax节点的文件路径。下面是一个我希望能够使用它的示例 IEnumerable<SyntaxNode> allNodes = compilation.SyntaxTrees.SelectMany(s => s.GetRoot().DescendantNodes()); IEnumerable<ClassDeclarationSyntax> allClasses

标题很好地解释了这一点。我正在编写一个源代码生成器,想知道如何找到给定
ClassDeclarationSyntax
节点的文件路径。下面是一个我希望能够使用它的示例

IEnumerable<SyntaxNode> allNodes = compilation.SyntaxTrees.SelectMany(s => s.GetRoot().DescendantNodes());
IEnumerable<ClassDeclarationSyntax> allClasses = allNodes.Where(d => d.IsKind(SyntaxKind.ClassDeclaration))
                                                         .OfType<ClassDeclarationSyntax>();
IEnumerable<string> filePaths = allClasses.Select(x=> x.GetFilePath());
IEnumerable allNodes=compilation.SyntaxTrees.SelectMany(s=>s.GetRoot().degenantNodes());
IEnumerable allClasses=allNodes.Where(d=>d.IsKind(SyntaxKind.ClassDeclaration))
.of type();
IEnumerable filepath=allClasses.Select(x=>x.GetFilePath());

您可以使用以下代码获取包含的文件路径:

SyntaxNode=。。。;
_=node.SyntaxTree.FilePath;
_=node.GetLocation().SourceTree?.FilePath//SourceTree可以为空