Пример #1
0
        private static void InjectParameterInputCheck(ParameterDefinition parameter, MethodDefinition method, ModuleReferences moduleReferences)
        {
            var instructions = method.Body.Instructions;
            var instruction0 = instructions[0];
            int index        = 0;

            instructions.Insert(index++, Instruction.Create(OpCodes.Ldarg, parameter));
            ILHelpers.InsertGetValueRef(ref index, instructions, parameter.ParameterType);
            ILHelpers.InsertThrowHelperCallIfValueRefIsNull(
                ref index, instructions, moduleReferences.ThrowArgumentNullMethod, parameter.Name, instruction0);
        }
        private static void InjectReturnValueCheck(MethodDefinition method, ReturnBlockInfo returnBlockInfo, ModuleReferences moduleReferences)
        {
            var instructions           = method.Body.Instructions;
            var returnBlockStartPoints = returnBlockInfo.NewStartPoints;

            for (int i = 0; i < returnBlockStartPoints.Length; i++)
            {
                Instruction injectionPoint = returnBlockStartPoints[i];
                int         index          = instructions.LastIndexOf(injectionPoint);

                // Value on stack is the return value
                var firstInstruction = Instruction.Create(OpCodes.Dup);
                instructions.Insert(index++, firstInstruction);
                ILHelpers.InsertGetValueRef(ref index, instructions, method.ReturnType);

                const string message = "Return value nullability contract was broken.";
                ILHelpers.InsertThrowHelperCallIfValueRefIsNull(
                    ref index, instructions, moduleReferences.ThrowOutputNullMethod, message, injectionPoint, returnBlockInfo);

                returnBlockStartPoints[i] = firstInstruction;
            }
        }
Пример #3
0
        private static void InjectParameterOutputCheck(ParameterDefinition parameter, MethodDefinition method, ReturnBlockInfo returnBlockInfo, ModuleReferences moduleReferences)
        {
            var instructions           = method.Body.Instructions;
            var returnBlockStartPoints = returnBlockInfo.NewStartPoints;

            for (int i = 0; i < returnBlockStartPoints.Length; i++)
            {
                Instruction injectionPoint = returnBlockStartPoints[i];
                int         index          = instructions.LastIndexOf(injectionPoint);

                var firstInstruction = Instruction.Create(OpCodes.Ldarg, parameter);
                instructions.Insert(index++, firstInstruction);
                ILHelpers.InsertGetValueRef(ref index, instructions, parameter.ParameterType);

                string message = $"Output parameter '{parameter.Name}' nullability contract was broken.";
                ILHelpers.InsertThrowHelperCallIfValueRefIsNull(
                    ref index, instructions, moduleReferences.ThrowOutputNullMethod, message, injectionPoint, returnBlockInfo);

                returnBlockStartPoints[i] = firstInstruction;
            }
        }