从调试版本中的类型获取文件的源代码文件名
给定 .NET 中的 Type 对象,我是否可以获得源代码文件名?我知道这仅在调试版本中可用,这很好,我也知道我可以使用 StackTrace 对象来获取调用堆栈中特定帧的文件名,但这不是我想要的。
但是我是否可以获取给定 System.Type 的源代码文件名?
Given a Type object in .NET, is it possible for me to get the source code filename? I know this would only be available in Debug builds and that's fine, I also know that I can use the StackTrace object to get the filename for a particular frame in a callstack, but that's not what I want.
But is it possible for me to get the source code filename for a given System.Type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的问题。我需要仅使用类的类型和方法名称来查找特定方法的文件名和行号。我使用的是旧的 mono .net 版本(unity 4.6)。我使用了 cecil 库,它提供了一些帮助程序来分析您的 pbd(或 mono 的 mdb)并分析调试符号。 https://github.com/jbevain/cecil/wiki
对于给定的类型和方法:
我找到了这个解决方案:
我知道有点晚了:)但我希望这会对某人有所帮助!
I encountered a similar problem. I needed to found the file name and line number of a specific method with only the type of the class and the method name. I'm on an old mono .net version (unity 4.6). I used the library cecil, it provide some helper to analyse your pbd (or mdb for mono) and analyse debug symbols. https://github.com/jbevain/cecil/wiki
For a given type and method:
I found this solution:
I know it's a bit late :) but I hope this will help somebody!
考虑到至少 C# 支持分部类声明(例如,两个源代码文件中的
publicpartial class Foo
),“给定 System.Type 的源代码文件名”意味着什么?如果类型声明分布在同一程序集中的多个源代码文件中,则源代码中不存在声明开始的单个点。@Darin Dimitrov:这看起来不像“如何获取源文件名和类型成员的行号?”的重复。对我来说,因为这是关于类型成员,而这个问题是关于类型。
Considering that at least C# supports partial class declarations (say,
public partial class Foo
in two source code files), what would "the source code filename for a given System.Type" mean? If a type declaration is spread out over multiple source code files within the same assembly, there is no single point in the source code where the declaration begins.@Darin Dimitrov: This doesn't seem like a duplicate of "How to get the source file name and the line number of a type member?" to me, since that is about a type member, whereas this question is about a type.
我使用这样的代码从正在运行的方法获取源文件和行号:
I've used code like this to get a source file and line number from the method that is running: