Click to Rate and Give Feedback
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
MarshalAsAttribute Class

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

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, Inherited := False)> _
Public NotInheritable Class MarshalAsAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As MarshalAsAttribute
C#
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, Inherited = false)]
public sealed class MarshalAsAttribute : Attribute
Visual C++
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, Inherited = false)]
public ref class MarshalAsAttribute sealed : public Attribute
JScript
public final class MarshalAsAttribute extends Attribute

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.

t167a1e9.alert_note(en-us,VS.90).gifNote:

The MarshalAsAttribute does not support marshaling of generic types.

The following examples show the placement of the MarshalAsAttribute in managed source code as applied to parameters, field, and return values.

Visual Basic
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()

    End Sub

    'Applied to a parameter.
    Public Sub M1(<MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal msg As String)
        msg = msg + "Goodbye"
    End Sub

    'Applied to a field within a class.
    Class MsgText
        <MarshalAsAttribute(UnmanagedType.LPWStr)> Public msg As String
    End Class

    'Applied to a a return value.
    Public Function M2() As <MarshalAsAttribute(UnmanagedType.LPWStr)> String
        Return "Hello World"
    End Function

End Module

C#
using System;
using System.Text;
using System.Runtime.InteropServices;


class Program
{

//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 = "Hello World";
                }

//Applied to a return value.
[return: MarshalAs(UnmanagedType.LPWStr)]
    public String GetMessage()
    {
        return "Hello World";
    }


static void Main(string[] args)
    {  }
}

System..::.Object
  System..::.Attribute
    System.Runtime.InteropServices..::.MarshalAsAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Applying MarshalAsAttribute to properties      Alfred Myers   |   Edit   |  

Visual Basic allows you to apply the attribute to properties.

Public Property Foo() As <MarshalAs(UnmanagedType.Currency)> Decimal
Get
Return Me._foo
End Get
Set(<MarshalAs(UnmanagedType.Currency)> ByVal value As Decimal)
Me._foo = value
End Set
End Property
Private _foo As Decimal

It is possible to apply the attribute to properties in C# with the following syntax:

 public decimal Foo { 
[return: MarshalAs(UnmanagedType.Currency)]
get {
return this.foo;
}
[param: MarshalAs(UnmanagedType.Currency)]
set {
this.foo = value;
}
}
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker