Esempio n. 1
0
        private static void CopyInstructions(MethodDefinition sourceMethod, MethodDefinition targetMethod, ReferenceResolver resolver)
        {
            TypeDefinition targetType = targetMethod.DeclaringType;

            var processor = targetMethod.Body.GetILProcessor();
            var offset = 0;
            foreach (var instruction in sourceMethod.Body.Instructions)
            {
                object operand;

                if (instruction.Operand is FieldReference)
                {
                    operand = resolver.ReferenceField((FieldReference)instruction.Operand, targetMethod, targetType);
                }
                else if (instruction.Operand is MethodReference)
                {
                    operand = resolver.ReferenceMethod((MethodReference)instruction.Operand, targetMethod, targetType);
                }
                else if (instruction.Operand is TypeReference)
                {
                    operand = resolver.ReferenceType((TypeReference)instruction.Operand, targetMethod, targetType);
                }
                else
                {
                    operand = instruction.Operand;
                }

                Instruction newInstruction = Helper.CreateInstruction(instruction.OpCode, instruction.OpCode.OperandType, operand);
                newInstruction.SequencePoint = instruction.SequencePoint;

                newInstruction.Offset = offset;
                offset += newInstruction.GetSize();

                processor.Append(newInstruction);
            }

            FixBranchingTargets(targetMethod.Body);
            CopyExceptionHandlers(sourceMethod, targetMethod, resolver);
        }