【发布时间】:2019-02-11 21:56:34
【问题描述】:
我试图在下面的方法中获取与指令对象关联的行号。 SequencePoint.StartLine 应该为我提供所需的信息,但在方法的注释掉部分中,seqPoint 始终为空。
/// <summary>
/// Finds all places in code where one or more methods are called.
/// </summary>
/// <param name="classOfMethods">Full name of the class that contains the methods to find.</param>
/// <param name="methodNames">Names of the methods to find in code.</param>
public MethodCall[] FindAllMethodCalls(Type classOfMethods, params string[] methodNames)
{
#region Use Mono.Cecil to find all instances where methods are called
var methodCalls = new List<MethodCall>();
var ad = AssemblyDefinition.ReadAssembly(binaryFileToSearch, new ReaderParameters { ReadSymbols = true });
foreach (var module in ad.Modules)
{
foreach (var type in module.GetTypes())
{
foreach (var method in type.Methods.Where(x => x.HasBody))
{
var instrs = method.Body.Instructions.Where(x => x.OpCode == OpCodes.Call).ToArray();
foreach (var instr in instrs)
{
var mRef = instr.Operand as MethodReference;
if (mRef != null && mRef.DeclaringType.FullName == classOfMethods.FullName && methodNames.Contains(mRef.Name))
{
// this does not work -- always returns null
//var seqPoint = method.DebugInformation.GetSequencePoint(instr);
//if (seqPoint != null)
//{
//}
methodCalls.Add(new MethodCall
{
CodeFile = method.DebugInformation.SequencePoints.First().Document.Url,
MethodRef = mRef,
});
}
}
}
}
}
...
return methodCalls.ToArray();
}
我调用 AssemblyDefinition.ReadAssembly() 的二进制文件具有相应的 .pdb 文件,并且我使用的是 ReadSymbols = true 选项。我做错了什么?
【问题讨论】:
-
你可以从源代码编译Mono Cecil,然后调试进去,github.com/jbevain/cecil我想知道你是否使用了错误的API。
-
我使用 Nuget 添加了最新的稳定 Mono.Cecil。
标签: c# mono.cecil