[英]Parsing source file and trying to get its methods doesn't work. Why is it not working?
private static void Main(string[] args)
{
var syntaxTree = CSharpSyntaxTree.ParseText(@"C:\Users\Umut\source\repos\frmMain\Program.cs");
var tree = syntaxTree.GetRoot();
var methods = tree.DescendantNodes().OfType<MethodDeclarationSyntax>().ToList();
foreach (var method in methods)
{
Console.WriteLine(method.Identifier);
}
}
I have this code and I am trying to read the methods out of the source code.我有这段代码,我正在尝试从源代码中读取方法。 When I run it with this:
当我用这个运行它时:
foreach (var method in methods)
{
Console.WriteLine(method.Identifier);
}
it's not working.它不工作。 Console just shows nothing (well, except for the press any key text).
控制台什么也没显示(好吧,除了按任意键文本)。 So it does compile but doesn't print out anything.
所以它确实编译但不打印任何东西。 Can anyone explain me why?
谁能解释我为什么?
EDIT: I am just stupid.编辑:我只是愚蠢。 I am using parsetext on a file, I needed ParseFile and have to use it like that:
我在文件上使用 parsetext,我需要 ParseFile 并且必须像这样使用它:
var path = @"C:\Users\Umut\source\repos\frmMain\frmMain1.cs";
using (var stream = File.OpenRead(path))
{
var syntaxTree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
I am just stupid.我只是愚蠢。 I am using parsetext on a file, I needed ParseFile and have to use it like that:
我在文件上使用 parsetext,我需要 ParseFile 并且必须像这样使用它:
var path = @"C:\Users\Umut\source\repos\frmMain\frmMain1.cs";
using (var stream = File.OpenRead(path))
{
var syntaxTree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.