Export (0) Print
Expand All

Marshal.CreateWrapperOfType Method (Object, Type)

Wraps the specified COM object in an object of the specified type.

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

public static Object CreateWrapperOfType(
	Object o,
	Type t
)

Parameters

o
Type: System.Object

The object to be wrapped.

t
Type: System.Type

The type of wrapper to create.

Return Value

Type: System.Object
The newly wrapped object that is an instance of the desired type.

ExceptionCondition
ArgumentException

t must derive from __ComObject.

-or-

t is a Windows Runtime type.

ArgumentNullException

The t parameter is null.

InvalidCastException

o cannot be converted to the destination type because it does not support all required interfaces.

CreateWrapperOfType converts one COM class type, typically the generic __ComObject type, to another COM class type. The input COM object, represented by parameter o, is a Runtime Callable Wrapper (RCW).

Both the t and o parameters must be classes whose signatures are attributed with System.Runtime.InteropServices.ComImportAttribute. The Tlbimp.exe (Type Library Importer) tool applies this attribute for you when it imports a type library. If you create the RCW manually in source code, you should apply this attribute to the managed signature that represents the original coclass to signify its COM origins.

Tlbimp.exe imports a COM coclass as a managed class and an interface. The coclass interface has the same name as the original coclass, and the managed class has the original coclass name appended with "Class". For example, a coclass called MyCoclass becomes a coclass interface called MyCoclass and a managed class called MyCoclassClass. Since t must be a class, not an interface, be sure to specify the managed class (MyCoclassClass) and not the coclass interface.

NoteNote

You lose the identity of the input COM object because a new RCW instance wraps the IUnknown pointer exposed by the original RCW.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Obsolete (compiler warning) in Windows Phone 8.1

  • SecurityCriticalAttribute 

    requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft