如何解析 C# 程序集并提取每个方法
对于我的应用程序,我想解析程序集并提取每个方法,并将方法名称和源代码存储在我的一个对象中(我在代码中定义)。
我应该从哪里开始实施?
编辑:从答案和评论我看到从程序集中获取源代码并不那么容易。那么如果我想从当前解决方案(Visual Studio)中没有的源代码文件中获取源代码,我应该从哪里开始呢?
For my application I'd like to parse through an assembly and extract every method and store the name of the method and the source code in one of my objects (I defined in my code).
where should I start to implement that?
EDIT: From the answers & comments i saw that it is not so easy to get source code from the assemblies. Then where should I start if I want to get source code from source code files that are not in my current solution (visual studio)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要检索此调试信息,请使用通用编译器基础结构:元数据 API 项目。
允许您读取
.pdb
文件(包含源文件和行号等调试信息的文件)。To retrieve this debugging information, use the Common Compiler Infrastructure: Metadata API project.
This allows you to read the
.pdb
files (the files containing the debugging information like source file and line number).首先,您需要找到所有类型。
请参阅此处执行此操作。
然后您可能需要找到每种类型的所有方法。
请参阅此处来执行此操作。
如果您需要一个示例来将所有内容整合在一起,请告诉我。
First you need to find all the types.
See here to do that.
Then you can need to find all the methods for each types.
See here to do that.
If you need an example to bring it all together let me know.
我不确定如何获取源代码,但如果您想获取有关方法的信息,则应该使用反射。
I'm not sure how to get the source code, but if you're trying to get information about the methods, you should use reflection.
该程序集不包含源代码。您也许能够提取 IL,但如果没有 Reflector 或 ildasm 等工具,这并不是特别有帮助。
要获取方法名称,只需类似以下内容:
The assembly doesn't contain the source code. You might be able to extract the IL, but without tools like reflector or ildasm that isn't especially helpful.
To obtain the method names, just something like: