Ejemplo n.º 1
0
        private void EmitILForMethod(ILGenerator ilGenerator, Instruction instruction, MemberInfo memberInfo)
        {
            MethodInfo methodInfo = memberInfo as MethodInfo;
            MethodBody methodBody = methodInfo.GetMethodBody();

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

            if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt)
            {
                DynamicMethod stub = instruction.OpCode == OpCodes.Call ?
                                     Stubs.GenerateStubForMethod(methodInfo) : Stubs.GenerateStubForVirtualMethod(methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType);
                ilGenerator.Emit(OpCodes.Call, stub);
            }
            else if (instruction.OpCode == OpCodes.Ldftn)
            {
                DynamicMethod stub = Stubs.GenerateStubForMethodPointer(methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType);
                ilGenerator.Emit(OpCodes.Call, stub);
            }
            else
            {
                ilGenerator.Emit(instruction.OpCode, methodInfo);
            }
        }
Ejemplo n.º 2
0
        private void EmitILForMethod(ILGenerator ilGenerator, Instruction instruction, MemberInfo memberInfo, bool overrideVirtual = false)
        {
            MethodInfo methodInfo = memberInfo as MethodInfo;

            if (PoseContext.StubCache.TryGetValue(methodInfo, out DynamicMethod stub))
            {
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType);
                ilGenerator.Emit(OpCodes.Call, stub);
                return;
            }

            MethodBody methodBody = methodInfo.GetMethodBody();

            if (methodBody == null && !methodInfo.IsAbstract)
            {
                ilGenerator.Emit(instruction.OpCode, methodInfo);
                return;
            }

            if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt)
            {
                stub = instruction.OpCode == OpCodes.Call || overrideVirtual?
                       Stubs.GenerateStubForMethod(methodInfo) : Stubs.GenerateStubForVirtualMethod(methodInfo);

                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType);
                ilGenerator.Emit(OpCodes.Call, stub);
                PoseContext.StubCache.TryAdd(methodInfo, stub);
            }
            else if (instruction.OpCode == OpCodes.Ldftn)
            {
                stub = Stubs.GenerateStubForMethodPointer(methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo);
                ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType);
                ilGenerator.Emit(OpCodes.Call, stub);
                PoseContext.StubCache.TryAdd(methodInfo, stub);
            }
            else
            {
                ilGenerator.Emit(instruction.OpCode, methodInfo);
            }
        }