获取程序集的原始源代码位置 [英] Get the original source code location for an assembly

查看:36
本文介绍了获取程序集的原始源代码位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过反射获取程序集的原始源代码位置的位置?

Is there a way to obtain the location of an assembly's original source code location through reflection?

警告,我不是在寻找程序集的当前位置,而是该位置是编译时源代码所在的位置.

Warning, I'm not looking for the current location of the assembly, rather the location were the source code resided when it was compiled.

例如

Given: myAssembly.dll
   c:\program files\myapp\myAssembly.dll <- I'm NOT looking for this location. This is its current location.
   d:\dev\myapp\main.cs <- this is the location I want; the location were the source code resided when it was compiled

我从这里开始,但是到目前为止,我还没有找到哪个兔子洞可以得到该信息.

I started with this, but I haven't been able find which rabbit hole to go down to get this info so far.

Assembly.GetExecutingAssembly().GetType("myAssembly.Main").<something>


.Net中发生异常时,您通常会看到引发异常的类的名称以及原始源代码文件的完整路径.这就是我所追求的.我意识到,可能需要一个.pdb符号文件才能获得此位置.


When exceptions occur in .Net you'll often see the name of the class that threw the exception along with the full path to the original source code file. This is what I'm after. I realize that a .pdb symbol file may be required in order to obtain this location.

推荐答案

该信息存储在.PDB文件中.它有两种基本风格,一种是从Debug版本获得的完整版本,另一种是 stripped ,您默认从Release版本获得的版本.剥离的.PDB删除了文件和行号信息.专门删除大多数公司认为专有的细节.

That information is stored in the .PDB file. There are two basic flavors of it, the full one that you get from a Debug build and the stripped one that you get by default from the Release build. A stripped .PDB has the file and line number info removed. Pretty specifically to remove details that most companies consider proprietary.

项目+属性,构建"选项卡,高级"按钮,调试信息"对此进行设置."Full"是常规的Debug构建设置,"pdb-only"是生成剥离版本的默认Release构建设置.

Project + Properties, Build tab, Advanced button, Debug Info sets this. "Full" is the normal Debug build setting, "pdb-only" is the default Release build setting that produces the stripped version.

DbgHelp api (原生样式). DIA SDK 为此提供了更友好的COM包装器以及从中选择的通常选择C#.CLR也使用它,这就是您如何在异常的堆栈跟踪中查看文件和行的方式.

Reading the .PDB file is supported by the DbgHelp api, the native flavor. The DIA SDK provides a friendlier COM wrapper for it and the usual choice from C#. The CLR uses it too, that's how you can see the file+line in an exception's stack trace.

这篇关于获取程序集的原始源代码位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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