Exemplo n.º 1
0
        private void AddMethod32(MethodWrapper value)
        {
            value.Offset = this.CurrentOffset;
            this.Write32(0);
            this.AddString32(value.Name);
            this.Write32(0);
            this.Write32((int)value.Flags);
            this.Write32(value.DeclaringType.Offset);
            this.Write32(value.ReturnType.Offset);
            int num = (value.Parameters != null) ? value.Parameters.Length : 0;

            this.Write32(num);
            this.Write32(0);
        }
        private static MethodDefinition Process(ModuleContext moduleContext, MethodWrapper methodWrapper, int index)
        {
            TypeReference    corLibType   = moduleContext.GetCorLibType("System.Int64");
            MethodReference  method       = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsGCHandleToObjectMethod);
            MethodReference  reference3   = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsGCHandleToPinnedArrayObjectMethod);
            MethodReference  reference4   = moduleContext.Import(moduleContext.OperationContext.GCHandledObjectsObjectToGCHandleMethod);
            MethodReference  corLibMethod = moduleContext.GetCorLibMethod("System.Runtime.InteropServices.Marshal", <> c.< > 9__7_0 ?? (< > c.< > 9__7_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < Process > b__7_0)));
            MethodDefinition targetMethod = methodWrapper.TargetMethod;
            MethodDefinition definition2  = new MethodDefinition(string.Format("$Invoke{0}", index), MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, corLibType);

            methodWrapper.InvokeMethod = definition2;
            ParameterDefinition item        = new ParameterDefinition("instance", ParameterAttributes.None, corLibType);
            ParameterDefinition definition4 = new ParameterDefinition("args", ParameterAttributes.None, new PointerType(corLibType));

            definition2.Parameters.Add(item);
            definition2.Parameters.Add(definition4);
            ILProcessor iLProcessor = definition2.Body.GetILProcessor();

            if (targetMethod.HasThis)
            {
                iLProcessor.EmitLdarg(item);
                iLProcessor.Emit(OpCodes.Call, method);
                if (targetMethod.DeclaringType.MetadataType != MetadataType.Object)
                {
                    iLProcessor.Emit(OpCodes.Castclass, moduleContext.Import(methodWrapper.DeclaringType.Type));
                }
            }
            for (int i = 0; i < targetMethod.Parameters.Count; i++)
            {
                TypeDefinition      definition6;
                ParameterDefinition definition5 = targetMethod.Parameters[i];
                iLProcessor.EmitLdarg(definition4);
                if (i != 0)
                {
                    iLProcessor.EmitLdc_I4(i * 8);
                    iLProcessor.Emit(OpCodes.Add);
                }
                switch (definition5.ParameterType.MetadataType)
                {
                case MetadataType.Boolean:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I1);
                    continue;
                }

                case MetadataType.Byte:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I1);
                    continue;
                }

                case MetadataType.Int32:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I4);
                    continue;
                }

                case MetadataType.Int64:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    continue;
                }

                case MetadataType.Single:
                {
                    iLProcessor.Emit(OpCodes.Ldind_R4);
                    continue;
                }

                case MetadataType.Double:
                {
                    iLProcessor.Emit(OpCodes.Ldind_R8);
                    continue;
                }

                case MetadataType.String:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    iLProcessor.Emit(OpCodes.Call, corLibMethod);
                    continue;
                }

                case MetadataType.ValueType:
                {
                    definition6 = definition5.ParameterType.Resolve();
                    if (!definition6.IsEnum)
                    {
                        break;
                    }
                    iLProcessor.Emit(OpCodes.Ldind_I4);
                    continue;
                }

                case MetadataType.Class:
                case MetadataType.GenericInstance:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    iLProcessor.Emit(definition5.ParameterType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, moduleContext.Import(definition5.ParameterType));
                    continue;
                }

                case MetadataType.Array:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, reference3);
                    iLProcessor.Emit(OpCodes.Castclass, moduleContext.Import(definition5.ParameterType));
                    continue;
                }

                case MetadataType.IntPtr:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    continue;
                }

                case MetadataType.Object:
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    continue;
                }

                default:
                    throw new NotSupportedException(string.Format("Parameter type {0} is not supported.", definition5.ParameterType.FullName));
                }
                if (!IsPodStruct(definition6))
                {
                    iLProcessor.Emit(OpCodes.Ldind_I8);
                    iLProcessor.Emit(OpCodes.Call, method);
                    iLProcessor.Emit(OpCodes.Unbox_Any, moduleContext.Import(definition5.ParameterType));
                }
                else
                {
                    iLProcessor.Emit(OpCodes.Ldind_I);
                    iLProcessor.Emit(OpCodes.Ldobj, moduleContext.Import(definition5.ParameterType));
                }
            }
            iLProcessor.Emit(targetMethod.IsVirtual ? OpCodes.Callvirt : OpCodes.Call, moduleContext.Import(methodWrapper.InvokeTargetMethod));
            switch (targetMethod.ReturnType.MetadataType)
            {
            case MetadataType.Void:
                iLProcessor.EmitLdc_I4(-1);
                iLProcessor.Emit(OpCodes.Conv_I8);
                break;

            case MetadataType.Boolean:
            case MetadataType.Byte:
            case MetadataType.Int32:
            case MetadataType.Int64:
            case MetadataType.Single:
            case MetadataType.ValueType:
                iLProcessor.Emit(OpCodes.Box, moduleContext.Import(targetMethod.ReturnType));
                iLProcessor.Emit(OpCodes.Call, reference4);
                break;

            case MetadataType.String:
            case MetadataType.Class:
            case MetadataType.Array:
            case MetadataType.GenericInstance:
            case MetadataType.Object:
                if (targetMethod.ReturnType.IsValueType)
                {
                    iLProcessor.Emit(OpCodes.Box, moduleContext.Import(targetMethod.ReturnType));
                }
                iLProcessor.Emit(OpCodes.Call, reference4);
                break;

            default:
                throw new NotSupportedException(string.Format("Return type {0} is not supported.", targetMethod.ReturnType.FullName));
            }
            iLProcessor.Emit(OpCodes.Ret);
            return(definition2);
        }