System.Runtime.InteropServices.MarshalAsAttribute Class

public sealed class MarshalAsAttribute : Attribute

Base Types

Object
  Attribute
    MarshalAsAttribute

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

Specifies how data is to be marshaled between managed and unmanaged code.

Description

This attribute can be applied to parameters, fields, or return values.

Each data type has a default marshaling behavior that is used if this attribute is not present. This attribute is only required when a type can be marshaled as more than one possible types. [Note: The UnmanagedType enumeration specifies possible data types.]

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

Attributes

AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple=false, Inherited=false)

See Also

System.Runtime.InteropServices Namespace

Members

MarshalAsAttribute Constructors

MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType) Constructor
MarshalAsAttribute(short) Constructor

MarshalAsAttribute Fields

MarshalAsAttribute.ArraySubType Field
MarshalAsAttribute.MarshalCookie Field
MarshalAsAttribute.MarshalType Field
MarshalAsAttribute.MarshalTypeRef Field
MarshalAsAttribute.SizeConst Field
MarshalAsAttribute.SizeParamIndex Field

MarshalAsAttribute Properties

MarshalAsAttribute.Value Property


MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType) Constructor

public MarshalAsAttribute(UnmanagedType unmanagedType);

Summary

Constructs and initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.

Parameters

unmanagedType
A UnmanagedType value indicating how the data is to be marshaled.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute(short) Constructor

public MarshalAsAttribute(short unmanagedType);

Summary

Constructs and initializes a new instance of the MarshalAsAttribute class with the specified value.

Parameters

unmanagedType
A Int16 containing a UnmanagedType value indicating how the data is to be marshaled.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.ArraySubType Field

public UnmanagedType ArraySubType;

Summary

A UnmanagedType enumeration value that specifies the type of elements in an array.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.MarshalCookie Field

public string MarshalCookie;

Summary

A String that is used with System.Runtime.InteropServices.UnmanagedType.CustomMarshaler to hold a cookie that is passed to the custom marshaler. The value of the cookie is defined by the custom marshaler implementation.

Description

This field is used only with System.Runtime.InteropServices.UnmanagedType.CustomMarshaler and is optional.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.MarshalType Field

public string MarshalType;

Summary

A String that identifies the Type of a custom marshaler.

Description

This field is used only with System.Runtime.InteropServices.UnmanagedType.CustomMarshaler and is required.

A custom marshaler is a type that is capable of marshaling calls from a specific managed type to a specific unmanaged type and vice versa.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.MarshalTypeRef Field

public Type MarshalTypeRef;

Summary

Specifies the managed Type of the argument being marshaled.

Description

This field is used only with System.Runtime.InteropServices.UnmanagedType.CustomMarshaler and is required.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.SizeConst Field

public int SizeConst;

Summary

A Int32 that specifies the size, in bytes, of the constant size array being marshaled.

Description

This field is used only with the System.Runtime.InteropServices.UnmanagedType.ByValArray or System.Runtime.InteropServices.UnmanagedType.LPArray enumeration value. When using either of these unmanaged types, either this field or System.Runtime.InteropServices.MarshalAsAttribute.SizeParamIndex is required.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.SizeParamIndex Field

public short SizeParamIndex;

Summary

A Int16 that identifies the position (starting with zero) of the method argument that identifies the size of the array being marshaled.

Description

This field is used only with the System.Runtime.InteropServices.UnmanagedType.ByValArray or System.Runtime.InteropServices.UnmanagedType.LPArray enumeration value. When using either of these unmanaged types, either this field or System.Runtime.InteropServices.MarshalAsAttribute.SizeConst is required.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace

MarshalAsAttribute.Value Property

public UnmanagedType Value { get; }

Summary

Gets the UnmanagedType value the data is to be marshaled as.

Property Value

The UnmanagedType value indicating how the data is to be marshaled.

Description

This property is read-only.

See Also

System.Runtime.InteropServices.MarshalAsAttribute Class, System.Runtime.InteropServices Namespace