public override MethodIL EmitIL()
        {
            try
            {
                PInvokeILCodeStreams pInvokeILCodeStreams = new PInvokeILCodeStreams();

                if (ThunkType == StructMarshallingThunkType.Cleanup)
                {
                    return(EmitCleanupIL(pInvokeILCodeStreams));
                }
                else
                {
                    return(EmitMarshallingIL(pInvokeILCodeStreams));
                }
            }
            catch (NotSupportedException)
            {
                string message = "Struct '" + ((MetadataType)ManagedType).Name +
                                 "' requires marshalling that is not yet supported by this compiler.";
                return(MarshalHelpers.EmitExceptionBody(message, this));
            }
            catch (InvalidProgramException ex)
            {
                Debug.Assert(!String.IsNullOrEmpty(ex.Message));
                return(MarshalHelpers.EmitExceptionBody(ex.Message, this));
            }
        }
示例#2
0
 public static MethodIL EmitIL(MethodDesc method, PInvokeILEmitterConfiguration pinvokeILEmitterConfiguration, InteropStateManager interopStateManager)
 {
     try
     {
         return(new PInvokeILEmitter(method, pinvokeILEmitterConfiguration, interopStateManager).EmitIL());
     }
     catch (NotSupportedException)
     {
         string message = "Method '" + method.ToString() +
                          "' requires non-trivial marshalling that is not yet supported by this compiler.";
         return(MarshalHelpers.EmitExceptionBody(message, method));
     }
     catch (InvalidProgramException ex)
     {
         Debug.Assert(!String.IsNullOrEmpty(ex.Message));
         return(MarshalHelpers.EmitExceptionBody(ex.Message, method));
     }
 }
示例#3
0
        public override MethodIL EmitIL()
        {
            try
            {
                Debug.Assert(_interopStateManager != null);
                Marshaller[]          marshallers          = new Marshaller[GetNumberOfInstanceFields()];
                MarshalAsDescriptor[] marshalAsDescriptors = ((MetadataType)ManagedType).GetFieldMarshalAsDescriptors();

                PInvokeFlags flags = new PInvokeFlags();
                if (ManagedType.PInvokeStringFormat == PInvokeStringFormat.UnicodeClass || ManagedType.PInvokeStringFormat == PInvokeStringFormat.AutoClass)
                {
                    flags.CharSet = CharSet.Unicode;
                }
                else
                {
                    flags.CharSet = CharSet.Ansi;
                }


                int index = 0;

                foreach (FieldDesc field in ManagedType.GetFields())
                {
                    if (field.IsStatic)
                    {
                        continue;
                    }

                    marshallers[index] = Marshaller.CreateMarshaller(field.FieldType,
                                                                     MarshallerType.Field,
                                                                     marshalAsDescriptors[index],
                                                                     (ThunkType == StructMarshallingThunkType.NativeToManage) ? MarshalDirection.Reverse : MarshalDirection.Forward,
                                                                     marshallers,
                                                                     _interopStateManager,
                                                                     index,
                                                                     flags,
                                                                     isIn: true,        /* Struct fields are considered as IN within the helper*/
                                                                     isOut: false,
                                                                     isReturn: false);
                    index++;
                }

                PInvokeILCodeStreams pInvokeILCodeStreams = new PInvokeILCodeStreams();

                if (ThunkType == StructMarshallingThunkType.Cleanup)
                {
                    return(EmitCleanupIL(pInvokeILCodeStreams, marshallers));
                }
                else
                {
                    return(EmitMarshallingIL(pInvokeILCodeStreams, marshallers));
                }
            }
            catch (NotSupportedException)
            {
                string message = "Struct '" + ((MetadataType)ManagedType).Name +
                                 "' requires non-trivial marshalling that is not yet supported by this compiler.";
                return(MarshalHelpers.EmitExceptionBody(message, this));
            }
            catch (InvalidProgramException ex)
            {
                Debug.Assert(!String.IsNullOrEmpty(ex.Message));
                return(MarshalHelpers.EmitExceptionBody(ex.Message, this));
            }
        }