Ejemplo n.º 1
0
        private void EmitILForMethod(ILGenerator ilGenerator, Instruction instruction, MethodInfo methodInfo)
        {
            if (methodInfo.InCoreLibrary())
            {
                // Don't attempt to rewrite unaccessible methods in System.Private.CoreLib/mscorlib
                if (!methodInfo.DeclaringType.IsPublic)
                {
                    goto forward;
                }
                if (!methodInfo.IsPublic && !methodInfo.IsFamily && !methodInfo.IsFamilyOrAssembly)
                {
                    goto forward;
                }
            }

            if (instruction.OpCode == OpCodes.Call)
            {
                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForDirectCall(methodInfo));
                return;
            }

            if (instruction.OpCode == OpCodes.Callvirt)
            {
                if (m_constrainedType != null)
                {
                    ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForVirtualCall(methodInfo, m_constrainedType));
                    m_constrainedType = null;
                    return;
                }

                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForVirtualCall(methodInfo));
                return;
            }

            if (instruction.OpCode == OpCodes.Ldftn)
            {
                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForDirectLoad(methodInfo));
                return;
            }

            if (instruction.OpCode == OpCodes.Ldvirtftn)
            {
                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForVirtualLoad(methodInfo));
                return;
            }

forward:
            ilGenerator.Emit(instruction.OpCode, methodInfo);
        }
Ejemplo n.º 2
0
        private void EmitILForConstructor(ILGenerator ilGenerator, Instruction instruction, ConstructorInfo constructorInfo)
        {
            if (constructorInfo.InCoreLibrary())
            {
                // Don't attempt to rewrite unaccessible constructors in System.Private.CoreLib/mscorlib
                if (!constructorInfo.DeclaringType.IsPublic)
                {
                    goto forward;
                }
                if (!constructorInfo.IsPublic && !constructorInfo.IsFamily && !constructorInfo.IsFamilyOrAssembly)
                {
                    goto forward;
                }
            }

            if (instruction.OpCode == OpCodes.Call)
            {
                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForDirectCall(constructorInfo));
                return;
            }

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

            if (instruction.OpCode == OpCodes.Ldftn)
            {
                ilGenerator.Emit(OpCodes.Call, Stubs.GenerateStubForDirectLoad(constructorInfo));
                return;
            }

            // If we get here, then we haven't accounted for an opcode.
            // Throw exception to make this obvious.
            throw new NotSupportedException(instruction.OpCode.Name);

forward:
            ilGenerator.Emit(instruction.OpCode, constructorInfo);
        }