CreateWrapperOfType(T, TWrapper) Method (T)
Collapse the table of content
Expand the table of content

Marshal.CreateWrapperOfType<'T, 'TWrapper> Method ('T)

 

[Supported in the .NET Framework 4.5.1 and later versions]

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

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

[<SecurityCriticalAttribute>]
static member CreateWrapperOfType<'T, 'TWrapper> : 
        o:'T -> 'TWrapper

Parameters

o
Type: T

The object to be wrapped.

Return Value

Type: TWrapper

The newly wrapped object.

Type Parameters

T

The type of object to wrap.

TWrapper

The type of object to return.

Exception Condition
ArgumentException

T must derive from __ComObject.

-or-

T is a Windows Runtime type.

InvalidCastException

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

CreateWrapperOfType<'T, 'TWrapper>('T) converts a COM object of type T to the TWrapper COM class type. The input COM object, represented by parameter o, is a Runtime Callable Wrapper (RCW).

Both the Tgeneric type parameter and the o parameter must be classes whose signatures are attributed with the System.Runtime.InteropServices.ComImportAttribute attribute. 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. BecauseT must be a class, not an interface, be sure to specify the managed class (MyCoclassClass) and not the coclass interface.

System_CAPS_noteNote

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

SecurityCriticalAttribute

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

Universal Windows Platform
Available since 8.1
.NET Framework
Available since 4.5.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft