Name

MarshalAs Attribute

Class

System.Runtime.InteropServices.MarshalAsAttribute

Applies to

Field, Parameter, ReturnValue

Description

Defines the correct type conversion between managed and unmanaged code. Unmanaged types are defined by the UnmanagedType enumeration, which is shown in the following table:

UnmanagedType

Value

Description

AnsiBStr

35

An ANSI BSTR (a character string whose first byte indicates the string length).

AsAny

40

Dynamic type determination at runtime.

Bool

2

4-byte Boolean (True <> 0, False = 0).

BStr

19

A Unicode BSTR (a character string whose first 2 bytes indicates the string length).

ByValArray

30

An array passed by value. An array that is a field in a structure must have this attribute. The SixeConst field must be set to the number of array elements, and the ArraySubType field can optionally be set to the unmanaged data type of the array.

ByValTStr

23

An inline fixed-length character array within a structure. The character type is determined by the CharSet argument of the containing structure’s <StructLayout> attribute.

Currency

15

A COM Currency data type. Used on the VB.NET and .NET Decimal data type.

CustomMarshaler

44

A custom marshaler class. The class is defined by the MarshalType or MarshelTypeRef field. Additional information can be passed to the custom marshaler by the MarshalCookie field.

Error

45

An HRESULT. The native .NET type should be a 4-byte signed or unsigned integer.

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.