This attribute is used on fields or parameters to indicate how the data should be marshaled between managed and unmanaged code. The attribute is always optional as each data type has default marshaling behavior. The attribute is only necessary when a given type can be marshaled to a number of other possible types. For example, a String could be marshaled to unmanaged code as either a LPSTR, LPWSTR, LPTSTR or as a BSTR. By default the string would be marshaled as a BSTR. The MarshalAsAttribute can be applied to an individual field or parameter to cause that particular string to be marshaled as a LPSTR instead of a BSTR. For complete details on using this attribute see the data type marshaling spec.
Object
Attribute
MarshalAsAttribute
[Visual Basic] Public Class MarshalAsAttribute Inherits Attribute [C#] public class MarshalAsAttribute : Attribute [C++] public __gc class MarshalAsAttribute : public Attribute [JScript] public class MarshalAsAttribute extends Attribute
Can be used to attribute parameters or fields.
In most cases, the attribute simply identifies the format of the unmanaged data using the UnmanagedType enumeration. For example:
[C#]
Void foo([MarshalAs(LPSTR) String s);
Some UnmanagedTypes require additional information. For example, additional information is needed when the UnmanagedType is LPARRAY. For a complete description of how to use this attribute, see the DataTypeMarshaling Spec.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib.dll
MarshalAsAttribute Members | System.Runtime.InteropServices Namespace