You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Mono.Cecil获取TypeDefinition对应的源文件位置?

使用Mono.Cecil查找类型定义对应的源文件

方法的可行实现

对于方法,我可以借助SequencePoint集合获取其对应的源文件。只要加载了PDB文件,就算是空方法也至少会包含一条nop指令,因此这个方案可行:

if (methodDefinition.DebugInformation.HasSequencePoints) {
  var firstSequencePoint = methodDefinition.DebugInformation.SequencePoints[0];
  return firstSequencePoint.Document.Url;
}

类型相关的疑问

但针对类型,我存在以下困惑:

  • PDB文件是否包含类型与源文档的映射关系?
  • 显然类型可能跨多个文档定义(比如分部类),这类信息是否可被获取?
  • 如果该信息可用,Mono.Cecil是否提供了对应的访问接口?

我曾尝试通过Mono.Cecil.Cil.PortablePdbReader读取模块的CustomDebugInformation,但查看原始数据后并未找到相关的有效信息。

内容的提问来源于stack exchange,提问作者David Airapetyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 21:05:19