Dela via


Custom Marshaling

A marshaler essentially provides a bridge between the functionality of old and new interfaces. Custom marshaling enables clients that you designed to work with an old interface to also work with servers that only implement a new interface. Custom marshaling also enables clients built to work with a new interface to work with servers that expose the old interface.

Instead of using the interop marshaler, you can design a custom marshaler for an interface that introduces different marshaling behavior or exposes the interface to the Component Object Model (COM) in a different way. By using a custom marshaler, you can minimize the distinction between new .NET Framework components and existing COM components.

For example, suppose that you are developing a managed interface called INew. When this interface is exposed to COM through a standard COM callable wrapper, it has the same methods as the managed interface and uses the marshaling rules built into the interop marshaler. Now suppose that a well-known COM interface called IOld already provides the same functionality as the INew interface. By introducing a custom marshaler, you can provide an unmanaged implementation of IOld that simply delegates the calls to the managed implementation of the INew interface. The custom marshaler essentially acts as a bridge between the managed and unmanaged interfaces.

Note that custom marshalers are not invoked when calling from the managed side to the unmanaged side on a dispatch-only interface.

Title

Description

Defining the Marshaling Type

Describes how to prepare the interfaces involved in setting up a custom marshaler.

Implementing the ICustomMarshaler Interface

Describes the options in preparing a custom marshaler.

Using a Substitute Marshaler

Describes how to use the MarshalAsAttribute attribute to insert a custom marshaler.

Interop Marshaling

Describes marshaling for COM interop and platform invoke.

Reference

System.Runtime.InteropServices.ICustomMarshaler

System.Runtime.InteropServices.MarshalAsAttribute