Export (0) Print
Expand All

MarshalAsAttribute Class

Indicates how to marshal the data between managed and unmanaged code.

For a list of all members of this type, see MarshalAsAttribute Members.

System.Object
   System.Attribute
      System.Runtime.InteropServices.MarshalAsAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Field Or _
   AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
NotInheritable Public Class MarshalAsAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter
   | AttributeTargets.ReturnValue)]
public sealed class MarshalAsAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Field |
   AttributeTargets::Parameter | AttributeTargets::ReturnValue)]
public __gc __sealed class MarshalAsAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter
   | AttributeTargets.ReturnValue)
class MarshalAsAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can apply this attribute to parameters, fields, or return values.

This attribute is optional, as each data type has a default marshaling behavior. This attribute is only necessary when a given type can be marshaled to multiple types. For example, you can marshal a string to unmanaged code as either a LPStr, a LPWStr, a LPTStr, or a BStr. By default, the common language runtime marshals a string parameter as a BStr to COM methods. You can apply the MarshalAsAttribute attribute to an individual field or parameter to cause that particular string to be marshaled as a LPStr instead of a BStr. The Type Library Exporter (Tlbexp.exe) passes your marshaling preferences to the common language runtime.

Some parameters and return values have different default marshaling behavior when used with COM interop or platform invoke. By default, the runtime marshals a string parameter (and fields in a value type) as a LPStr to a platform invoke method or function. For additional information, see Default Marshaling Behavior.

In most cases, the attribute simply identifies the format of the unmanaged data using the UnmanagedType enumeration, as shown in the following C# signature:

void

MyMethod([MarshalAs(LPStr)] String s);

Some UnmanagedType enumeration members require additional information. For example, additional information is needed when the UnmanagedType is LPArray. For a complete description of how to use this attribute with arrays, see Default Marshaling for Arrays.

The Type Library Importer (Tlbimp.exe) also applies this attribute to parameters, fields, and return values to indicate that the data type in the input type library is not the default type for the corresponding managed data type. Tlbimp.exe always applies the MarshalAsAttribute to String and Object types for clarity, regardless of the type specified in the input type library.

Example

[Visual Basic, C#] The following examples show the placement of the MarshalAsAttribute in managed source code as applied to parameters, field, and return values.

[Visual Basic] 
'Applied to a parameter.
  Public Sub M1 (<MarshalAs(UnmanagedType.LPWStr)> msg As String)
'Applied to a field within a class.
  Class MsgText 
    <MarshalAs(UnmanagedType.LPWStr)> Public msg As String
  End Class
'Applied to a a return value.
  Public Function M2() As <MarshalAs(UnmanagedType.LPWStr)> String
   
[C#] 
//Applied to a parameter.
  public void M1 ([MarshalAs(UnmanagedType.LPWStr)]String msg);
//Applied to a field within a class.
  class MsgText {
    [MarshalAs(UnmanagedType.LPWStr)] Public String msg;
  }
//Applied to a return value.
[return: MarshalAs(UnmanagedType.LPWStr)]
public String GetMessage();
   

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

MarshalAsAttribute Members | System.Runtime.InteropServices Namespace | UnmanagedType | Type Library Exporter (Tlbexp.exe) | Type Library Importer (Tlbimp.exe)

Show:
© 2014 Microsoft