简体   繁体   English

解析源文件并尝试获取其方法不起作用。 为什么它不起作用?

[英]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.

相关问题 无法将数据写入文件。 所有 System.IO 方法都不起作用。 Xamarin.Forms - Can't write data to a file. All System.IO methods doesn't work. Xamarin.Forms NuGet安装软件包不起作用。 如何获取更多详细信息以帮助调试为什么失败? - NuGet install-package doesn't work. How can I get more verbose information to help debug why this is failing? WPF DataTrigger不起作用。 - WPF DataTrigger doesn't work. 为什么装箱和拆箱不适用于方法? - Why doesn't boxing and unboxing work with methods? 无法使Delete方法起作用。 我试过调试,它似乎甚至没有直接指向该方法 - Cannot get Delete method to work. I've tried debugging and it doesn't seem to even direct to the method 设置注册表项不起作用。 有时 - Setting registry key doesn't work. Sometimes 自定义ValidationAttribute不起作用。 始终返回true - Custom ValidationAttribute doesn't work. Always returns true GridView绑定不起作用。 我想念什么? - GridView Binding doesn't work. What am I missing? WebRootPath的图像路径不起作用。 IHostingEnvironment - Image path with WebRootPath doesn't work. IHostingEnvironment 屏幕截图转换为灰度不起作用。 是什么导致这个? - Screenshot conversion into grayscale doesn't work. What causes this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM