Ejemplo n.º 1
0
        private void WeaveOnComplete(MethodDefinition targetMethod, ILProcessor ilp, VariableDefinition aspectInstance, VariableDefinition correlation)
        {
            if (_onComplete != null)
            {
                var last = targetMethod.Body.Instructions.Last();

                if (last.OpCode == OpCodes.Ret)
                {
                    var insertBefore = last.Previous;

                    while (insertBefore.OpCode != OpCodes.Nop && insertBefore.OpCode != OpCodes.Br_S)
                    {
                        insertBefore = insertBefore.Previous;
                    }

                    Instruction loadReturn = null;

                    //if it's a br_s then we are returning a value.
                    if (insertBefore.OpCode == OpCodes.Br_S)
                    {
                        loadReturn = ilp.Copy(insertBefore.Next);
                    }

                    var onCompleteInstructions = GetOnCompleteInstructions(targetMethod, ilp, aspectInstance, correlation, loadReturn).ToArray();

                    //Place this code before the last nop or br_s so that the onException can place itself correctly.

                    ilp.InsertInstructionsBefore(insertBefore, onCompleteInstructions);
                }
            }
        }