Upraviť

Zdieľať cez


MarshalAsAttribute Class

Definition

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

public ref class MarshalAsAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)]
public sealed class MarshalAsAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class MarshalAsAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)>]
type MarshalAsAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MarshalAsAttribute = class
    inherit Attribute
Public NotInheritable Class MarshalAsAttribute
Inherits Attribute
Inheritance
MarshalAsAttribute
Attributes

Examples

The following example applies the MarshalAsAttribute to a field, a method parameter, and a method return value in a managed type.

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)
    {  }
}
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 return value.
    Public Function M2() As <MarshalAsAttribute(UnmanagedType.LPWStr)> String
        Return "Hello World"
    End Function

End Module

The following example applies the MarshalAsAttribute attribute to a property:

decimal _money;   

public decimal Money 
{
   [return: MarshalAs(UnmanagedType.Currency)]
   get { return this._money; }
   [param: MarshalAs(UnmanagedType.Currency)]
   set { this._money = value; }
}
Dim _money As Decimal   

Public Property Money As <MarshalAs(UnmanagedType.Currency)> Decimal 
   Get
      Return Me._money
   End Get
   Set(<MarshalAs(UnmanagedType.Currency)> value As Decimal)
      Me._money = value
   End Set   
End Property

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 Tlbexp.exe (Type Library Exporter) 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:

public void SomeMethod([MarshalAs(UnmanagedType.LPStr)] String s)
Public Sub SomeMethod(<MarshalAs(UnmanagedType.LPStr)> s As String)

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 Tlbimp.exe (Type Library Importer) 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.

Note

The MarshalAsAttribute does not support marshaling of generic types.

Constructors

MarshalAsAttribute(Int16)

Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.

MarshalAsAttribute(UnmanagedType)

Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType enumeration member.

Fields

ArraySubType

Specifies the element type of the unmanaged LPArray or ByValArray.

IidParameterIndex

Specifies the parameter index of the unmanaged iid_is attribute used by COM.

MarshalCookie

Provides additional information to a custom marshaler.

MarshalType

Specifies the fully qualified name of a custom marshaler.

MarshalTypeRef

Implements MarshalType as a type.

SafeArraySubType

Indicates the element type of the SafeArray.

SafeArrayUserDefinedSubType

Indicates the user-defined element type of the SafeArray.

SizeConst

Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in a string to import.

SizeParamIndex

Indicates the zero-based parameter that contains the count of array elements, similar to size_is in COM.

Properties

TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value

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

Methods

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode()

Returns the hash code for this instance.

(Inherited from Attribute)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also