Dela via


Implementing the ICustomMarshaler Interface

To use a custom marshaler, you must apply the MarshalAsAttribute attribute to the parameter or field being marshaled. The attribute identifies the custom marshaler that activates the appropriate wrapper.

The custom marshaler implements the System.Runtime.InteropServices.ICustomMarshaler interface to provide the appropriate wrappers to the runtime, creating the custom marshaler the first time an argument needs to be marshaled. It calls the ICustomMarshaler.MarshalNativeToManaged and ICustomMarshaler.MarshalManagedToNative methods on the custom marshaler to activate the correct wrapper to handle the call. The following table describes each method exposed by the ICustomMarshaler interface.

Interface method

Description

MarshalNativeToManaged

Returns a custom runtime callable wrapper that can marshal the unmanaged interface passed as an argument. The marshaler should return an instance of the custom runtime callable wrapper for that type.

MarshalManagedToNative

Returns a custom COM callable wrapper that can marshal the managed interface passed as an argument. The marshaler should return an instance of the custom COM callable wrapper for that type.

CleanUpNativeData

Provides an opportunity for the custom marshaler to perform any necessary cleanup of the wrapper returned by the MarshalManagedToNative method.

CleanUpManagedData

Provides an opportunity for the custom marshaler to perform any necessary cleanup of the wrapper returned by the MarshalManagedToNative method.

GetNativeDataSize

Returns the size of the native data to be marshaled.

See Also

Concepts

Custom Marshaling Overview

Defining the Marshaling Type

Using a Substitute Marshaler

Change History

Date

History

Reason

July 2009

Added topic links

Information enhancement.