Пример #1
0
        public void GetParameters_InstructionsThatReadParameters_ReturnsParameters()
        {
            MethodReference method = CecilUtility.Import(() => string.Format("Test", 12));

            var instructions = new[]
            {
                Instruction.Create(OpCodes.Ldarg_1),
                Instruction.Create(OpCodes.Ldarg_0)
            };

            Parameter parameter = new Parameter(CecilUtility.Import(typeof(string)), 0);
            _instructionHelperMock
                .Setup(helper => helper.ReadsParameter(method, instructions[0], out parameter))
                .Returns(true);

            Parameter parameter2 = new Parameter(CecilUtility.Import(typeof(int)), 1);
            _instructionHelperMock
                .Setup(helper => helper.ReadsParameter(method, instructions[1], out parameter2))
                .Returns(true);

            var subject = new ParameterDeducer(_instructionHelperMock.Object);
            TypeReference[] result = subject.GetParameters(method, instructions);

            Assert.AreEqual(2, result.Length);
            Assert.AreEqual(parameter.ParameterType, result[0]);
            Assert.AreEqual(parameter2.ParameterType, result[1]);
        }
Пример #2
0
        public void Constructor_SetsProperties()
        {
            const int index = 42;
            var type = CecilUtility.Import(typeof(string));

            var subject = new Parameter(type, index);

            Assert.AreSame(type, subject.ParameterType);
            Assert.AreEqual(index, subject.Index);
        }
Пример #3
0
        public bool ReadsParameter(MethodReference method, Instruction instruction,
            out Parameter parameter)
        {
            var parameters = method.Parameters;
            int offset = method.HasThis ? 1 : 0;

            TypeReference parameterType = null;
            int index = -1;

            switch (instruction.OpCode.Code)
            {
                case Code.Ldarg_0:
                    parameterType = method.HasThis ? method.DeclaringType : parameters[0].ParameterType;
                    index = 0;
                    break;

                case Code.Ldarg_1:
                    parameterType = parameters[1 - offset].ParameterType;
                    index = 1;
                    break;

                case Code.Ldarg_2:
                    parameterType = parameters[2 - offset].ParameterType;
                    index = 2;
                    break;

                case Code.Ldarg_3:
                    parameterType = parameters[3 - offset].ParameterType;
                    index = 3;
                    break;

                case Code.Ldarg_S:
                case Code.Ldarg:
                case Code.Ldarga:
                case Code.Ldarga_S:
                    parameterType = ((ParameterReference)instruction.Operand).ParameterType;
                    index = ((ParameterReference)instruction.Operand).Index + offset;
                    break;
            }

            parameter = parameterType != null ? new Parameter(parameterType, index) : null;
            return parameter != null;
        }