Пример #1
0
        public void GenerateStubTestConstructor()
        {
            var info = typeof(ExampleClass).GetConstructor(new [] { typeof(int) });
            var stub = StubGenerator.GenerateStub(info);

            var result = (ExampleClass)stub.Invoke(null, new object[] { 3, new ShimContext() });

            Assert.NotNull(result);
            Assert.Equal(3, result.Factor);

            info = typeof(List <string>).GetConstructor(Type.EmptyTypes);
            stub = StubGenerator.GenerateStub(info);

            var resultList = (List <string>)stub.Invoke(null, new object[] { new ShimContext() });

            Assert.NotNull(resultList);
            Assert.Empty(resultList);

            info = typeof(ExampleStruct).GetConstructor(new[] { typeof(int) });
            stub = StubGenerator.GenerateStub(info);

            var resultStruct = (ExampleStruct)stub.Invoke(null, new object[] { 3, new ShimContext() });

            Assert.Equal(3, resultStruct.Factor);
        }
Пример #2
0
        private static void EmitILForConstructor(ILGenerator ilGenerator, OperandInstruction <MethodBase> instruction,
                                                 ConstructorInfo constructorInfo, int contextParamIndex)
        {
            /*if (PoseContext.StubCache.TryGetValue(constructorInfo, out DynamicMethod stub))
             * {
             *  ilGenerator.Emit(OpCodes.Ldtoken, constructorInfo);
             *  ilGenerator.Emit(OpCodes.Ldtoken, constructorInfo.DeclaringType);
             *  ilGenerator.Emit(OpCodes.Call, stub);
             *  return;
             * }*/

            var methodBody = constructorInfo.GetMethodBody();

            if (methodBody == null)
            {
                ilGenerator.Emit(instruction.OpCode, constructorInfo);
                return;
            }

            if (instruction.OpCode != OpCodes.Newobj && instruction.OpCode != OpCodes.Call)
            {
                ilGenerator.Emit(instruction.OpCode, constructorInfo);
                return;
            }

            var stub = StubGenerator.GenerateStub(constructorInfo, instruction.OpCode == OpCodes.Newobj);

            ilGenerator.Emit(OpCodes.Ldarg, contextParamIndex);
            ilGenerator.Emit(OpCodes.Call, stub);

            //PoseContext.StubCache.TryAdd(constructorInfo, stub);
        }