private CilInstructionCollection CreateDummyMethod(bool hasThis, int paramCount, int localCount)
        {
            var flags = hasThis
                ? MethodAttributes.Public
                : MethodAttributes.Public | MethodAttributes.Static;

            var parameterTypes = Enumerable.Repeat <TypeSignature>(_module.CorLibTypeFactory.Object, paramCount);

            var signature = hasThis
                ? MethodSignature.CreateInstance(_module.CorLibTypeFactory.Void, parameterTypes)
                : MethodSignature.CreateStatic(_module.CorLibTypeFactory.Void, parameterTypes);

            var method = new MethodDefinition("Dummy", flags, signature);

            var body = new CilMethodBody(method);

            for (int i = 0; i < localCount; i++)
            {
                body.LocalVariables.Add(new CilLocalVariable(_module.CorLibTypeFactory.Object));
            }

            method.MethodBody = body;

            return(body.Instructions);
        }
        private static MethodDefinition CreateDummyMethod(ModuleDefinition module, string name)
        {
            var method = new MethodDefinition(name,
                                              MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual
                                              | MethodAttributes.NewSlot,
                                              MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));

            return(method);
        }
        public void NewObjVoidMethodShouldPushSingleElement()
        {
            var type   = new TypeReference(_module, null, "SomeType");
            var member = new MemberReference(type, "SomeMethod",
                                             MethodSignature.CreateInstance(_module.CorLibTypeFactory.Int32));

            var instruction = new CilInstruction(CilOpCodes.Newobj, member);

            Assert.Equal(1, instruction.GetStackPushCount());
        }
        public void CallVoidMethodShouldPushZeroElements()
        {
            var type   = new TypeReference(_module, null, "SomeType");
            var member = new MemberReference(type, "SomeMethod",
                                             MethodSignature.CreateInstance(_module.CorLibTypeFactory.Void));

            var instruction = new CilInstruction(CilOpCodes.Call, member);

            Assert.Equal(0, instruction.GetStackPushCount());
        }
        public void CallInstanceWithZeroParametersShouldPopOneElement()
        {
            var method = new MethodDefinition("Method", MethodAttributes.Static,
                                              MethodSignature.CreateStatic(_module.CorLibTypeFactory.Int32));

            method.CilMethodBody = new CilMethodBody(method);

            var type   = new TypeReference(_module, null, "SomeType");
            var member = new MemberReference(type, "SomeMethod",
                                             MethodSignature.CreateInstance(_module.CorLibTypeFactory.Void));

            var instruction = new CilInstruction(CilOpCodes.Call, member);

            Assert.Equal(1, instruction.GetStackPopCount(method.CilMethodBody));
        }
        private static MethodDefinition CreateDummyMethod(ModuleDefinition module, string name, int parameterCount = 0)
        {
            var method = new MethodDefinition(name,
                                              MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual
                                              | MethodAttributes.NewSlot,
                                              MethodSignature.CreateInstance(module.CorLibTypeFactory.Void));

            for (ushort i = 1; i <= parameterCount; i++)
            {
                method.Signature.ParameterTypes.Add(module.CorLibTypeFactory.Object);
                method.ParameterDefinitions.Add(new ParameterDefinition(i, $"{name}Arg{i}", 0));
            }

            method.Parameters.PullUpdatesFromMethodSignature();

            return(method);
        }