Export (0) Print
Expand All

MethodBuilder.SetMarshal Method

Sets marshaling information for the return type of this method.

[Visual Basic]
Public Sub SetMarshal( _
   ByVal unmanagedMarshal As UnmanagedMarshal _
)
[C#]
public void SetMarshal(
 UnmanagedMarshal unmanagedMarshal
);
[C++]
public: void SetMarshal(
 UnmanagedMarshal* unmanagedMarshal
);
[JScript]
public function SetMarshal(
   unmanagedMarshal : UnmanagedMarshal
);

Parameters

unmanagedMarshal
Marshaling information for the return type of this method.

Exceptions

Exception Type Condition
InvalidOperationException The containing type was previously created using CreateType

Example

[Visual Basic, C#, C++] The code sample below illustrates the contextual usage of the SetMarshal method to marshal the results of a method call as a different type.

[Visual Basic] 
Dim myMethod As MethodBuilder = myDynamicType.DefineMethod("MyMethodReturnsMarshal", _
              MethodAttributes.Public, GetType(System.UInt32), _
              New Type() {GetType(String)}) 

' We want the return value of our dynamic method to be marshalled as 
' an 64-bit (8-byte) signed integer, instead of the default 32-bit
' unsigned int as specified above. The UnmanagedMarshal class can perform
' the type conversion.

Dim marshalMeAsI8 As UnmanagedMarshal = UnmanagedMarshal.DefineUnmanagedMarshal( _
                    System.Runtime.InteropServices.UnmanagedType.I8)

myMethod.SetMarshal(marshalMeAsI8)


[C#] 

    MethodBuilder myMethod = myDynamicType.DefineMethod("MyMethodReturnsMarshal",
                        MethodAttributes.Public,
                        typeof(uint),
                        new Type[] { typeof(string) });

    // We want the return value of our dynamic method to be marshalled as 
    // an 64-bit (8-byte) signed integer, instead of the default 32-bit
       // unsigned int as specified above. The UnmanagedMarshal class can perform
    // the type conversion.

    UnmanagedMarshal marshalMeAsI8 = UnmanagedMarshal.DefineUnmanagedMarshal(
                         System.Runtime.InteropServices.UnmanagedType.I8);    

    myMethod.SetMarshal(marshalMeAsI8);    


[C++] 
Type* temp0 [] = {__typeof(String)};
MethodBuilder*  myMethod = myDynamicType->DefineMethod(S"MyMethodReturnsMarshal",
   MethodAttributes::Public,
   __typeof(UInt32),
   temp0);

// We want the return value of our dynamic method to be marshalled as
// an 64-bit (8-Byte) signed integer, instead of the default 32-bit
// unsigned int as specified above. The UnmanagedMarshal class can perform
// the type conversion.

UnmanagedMarshal*  marshalMeAsI8 = UnmanagedMarshal::DefineUnmanagedMarshal(System::Runtime::InteropServices::UnmanagedType::I8);

myMethod->SetMarshal(marshalMeAsI8);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

MethodBuilder Class | MethodBuilder Members | System.Reflection.Emit Namespace

Show:
© 2014 Microsoft