private PInvokeILEmitter(MethodDesc targetMethod, PInvokeILEmitterConfiguration pinvokeILEmitterConfiguration)
        {
            Debug.Assert(targetMethod.IsPInvoke || targetMethod is DelegateMarshallingMethodThunk);

            _methodData  = new PInvokeMethodData(targetMethod, pinvokeILEmitterConfiguration);
            _marshallers = InitializeMarshallers(_methodData);
        }
        private static                     Marshaller[] InitializeMarshallers(PInvokeMethodData pInvokeMethodData)
        {
            MethodDesc      targetMethod = pInvokeMethodData.TargetMethod;
            bool            isDelegate   = targetMethod is DelegateMarshallingMethodThunk;
            MethodSignature methodSig    = isDelegate ? ((DelegateMarshallingMethodThunk)targetMethod).DelegateSignature : targetMethod.Signature;

            ParameterMetadata[] parameterMetadataArray = targetMethod.GetParameterMetadata();
            Marshaller[]        marshallers            = new Marshaller[methodSig.Length + 1];
            int parameterIndex = 0;
            ParameterMetadata parameterMetadata = new ParameterMetadata();

            for (int i = 0; i < marshallers.Length; i++)
            {
                Debug.Assert(parameterIndex == parameterMetadataArray.Length || i <= parameterMetadataArray[parameterIndex].Index);
                if (parameterIndex == parameterMetadataArray.Length || i < parameterMetadataArray[parameterIndex].Index)
                {
                    // if we don't have metadata for the parameter, create a dummy one
                    parameterMetadata = new ParameterMetadata(i, ParameterMetadataAttributes.None, null);
                }
                else if (i == parameterMetadataArray[parameterIndex].Index)
                {
                    parameterMetadata = parameterMetadataArray[parameterIndex++];
                }
                TypeDesc parameterType = (i == 0) ? methodSig.ReturnType : methodSig[i - 1];  //first item is the return type
                marshallers[i] = Marshaller.CreateMarshaller(parameterType,
                                                             pInvokeMethodData,
                                                             parameterMetadata,
                                                             marshallers,
                                                             isDelegate ? MarshalDirection.Reverse : MarshalDirection.Forward);
            }

            return(marshallers);
        }
Exemple #3
0
        private PInvokeILEmitter(MethodDesc targetMethod, PInvokeILEmitterConfiguration pinvokeILEmitterConfiguration)
        {
            Debug.Assert(targetMethod.IsPInvoke);

            _methodData  = new PInvokeMethodData(targetMethod, pinvokeILEmitterConfiguration, MarshalDirection.Forward);
            _marshallers = InitializeMarshallers(_methodData);
        }