如何获取cs文件中所有成员函数的列表 [英] How do I get a list of all member functions in a cs file

查看:121
本文介绍了如何获取cs文件中所有成员函数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有cs文件的路径。



我尝试了什么:



我在扫描整个文件后使用了基本的字符串操作来获取函数。我正在寻找一个好方法。

I have the path of the cs file.

What I have tried:

I have used basic string operations to get the functions after scanning the whole file. I'm looking for a good method.

推荐答案

如果你不能使用反射就没有好方法 - 你必须手动解析整个文件和确切地确定内容是什么。这意味着识别名称空间(因为一个文件可以合法地连接多个名称空间),类(因为一个同名的类可以存在于多个名称空间中,类可以嵌入其他类中),注释(因为C#包含/ *和//注释表单,并且用/ * ... * /块),#if块,#else块等注释掉整个类是有效的。



严重吗?使用反射。
There is no "good method" if you can't use reflection - you have to manually parse the whole file and establish exactly what the content is. That means identifying namespaces (because a file can legitimately contine multiple namespaces), classes (because a class of the same name can exist in multiple namespaces, and classes can be embedded inside other classes), comments (because C# includes /* and // comment forms and it's valid to comment out whole classes with a /* ... */ block), #if blocks, #else blocks, etc.

Seriously? Use reflection.


如果我这样做,我将使用Visual Studio SDK,它允许您构建在Visual Studio中工作的扩展和工具。这个SDK提供了直接在Visual Studio内部解析类的能力。
If I were doing this, I'd use the Visual Studio SDK that allows you to build extensions and tools that work inside Visual Studio. This SDK provides the ability to parse classes directly inside Visual Studio itself.


我倾向于使用Roslyn:

GitHub - dotnet / roslyn:.NET编译器平台(Roslyn)为开源C#和Visual Basic编译器提供了丰富的代码分析API。 [ ^ ]

罗斯林入门 - 第1部分 [ ^ ]



它是相同的编译器VS2015 / 2017使用,所以它应该处理你可以抛出的任何东西。
I'd be inclined to use Roslyn for this:
GitHub - dotnet/roslyn: The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs.[^]
Getting Started with Roslyn - Part 1[^]

It's the same compiler that VS2015/2017 use, so it should handle anything you can throw at it.


这篇关于如何获取cs文件中所有成员函数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆