Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how to inject a finally.</p> <p>First you need to fix your return statements. You only want one.</p> <pre><code>Instruction FixReturns() { if (Method.ReturnType == TypeSystem.Void) { var instructions = body.Instructions; var lastRet = Instruction.Create(OpCodes.Ret); instructions.Add(lastRet); for (var index = 0; index &lt; instructions.Count - 1; index++) { var instruction = instructions[index]; if (instruction.OpCode == OpCodes.Ret) { instructions[index] = Instruction.Create(OpCodes.Leave, lastRet); } } return lastRet; } else { var instructions = body.Instructions; var returnVariable = new VariableDefinition("methodTimerReturn", Method.ReturnType); body.Variables.Add(returnVariable); var lastLd = Instruction.Create(OpCodes.Ldloc, returnVariable); instructions.Add(lastLd); instructions.Add(Instruction.Create(OpCodes.Ret)); for (var index = 0; index &lt; instructions.Count - 2; index++) { var instruction = instructions[index]; if (instruction.OpCode == OpCodes.Ret) { instructions[index] = Instruction.Create(OpCodes.Leave, lastLd); instructions.Insert(index, Instruction.Create(OpCodes.Stloc, returnVariable)); index++; } } return lastLd; } } </code></pre> <p>Then find the first instruction. You will need to skip 2 if it is an instance constructor.</p> <pre><code>Instruction FirstInstructionSkipCtor() { if (Method.IsConstructor &amp;&amp; !Method.IsStatic) { return body.Instructions.Skip(2).First(); } return body.Instructions.First(); } </code></pre> <p>Then stitch it together</p> <pre><code>void InnerProcess() { body = Method.Body; body.SimplifyMacros(); ilProcessor = body.GetILProcessor(); var returnInstruction = FixReturns(); var firstInstruction = FirstInstructionSkipCtor(); var beforeReturn = Instruction.Create(OpCodes.Nop); ilProcessor.InsertBefore(returnInstruction, beforeReturn); InjectIlForFinaly(returnInstruction); var handler = new ExceptionHandler(ExceptionHandlerType.Finally) { TryStart = firstInstruction, TryEnd = beforeReturn, HandlerStart = beforeReturn, HandlerEnd = returnInstruction, }; body.ExceptionHandlers.Add(handler); body.InitLocals = true; body.OptimizeMacros(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload