2015-02-09 28 views
1

我正在开发一个fody插件(使用mono.cecil)并在方法开始时注入一些代码。我希望调试器跨越注入的代码。Fody/Mono.Cecil:在调试器中隐藏行

我在这里找到了一些信息:http://blogs.msdn.com/b/abhinaba/archive/2005/10/10/479016.aspx

所以我尝试更新的注入指令行号0xfeefee序列点。

我使用下面的代码这样做:

public static void Inject(MethodDefinition method, List<Instruction> instructions) 
    { 
     var methodInstructions = method.Body.Instructions; 

     int index = 0; 
     var sequencePoint = method.Body.Instructions 
      .Where(instruction => instruction.SequencePoint != null) 
      .Select(instruction => instruction.SequencePoint) 
      .FirstOrDefault(); 

     if (method.HasBody && sequencePoint != null && sequencePoint.Document != null) 
     { 
      var instruction = instructions[0]; 
      instruction.SequencePoint = new SequencePoint(sequencePoint.Document); 
      instruction.SequencePoint.StartLine = 0xfeefee; 
      instruction.SequencePoint.EndLine = 0xfeefee; 
     } 

     foreach (var instruction in instructions) 
     { 
      methodInstructions.Insert(index++, instruction); 
     } 

     method.Body.OptimizeMacros(); 
    } 

这应该是基本相同的代码NullGuard.Fody项目使用,但它不工作。在尝试调试代码注入的方法时,我仍然从Visual Studio获取源代码不可用的信息。

我是否需要做其他事情,以便更新pdb文件?

回答

1

尝试从查询中删除Where以选择第一个序列点。

如果方法的第一条指令有一个序列点,那么只需要添加隐藏的序列点。