System.Runtime.InteropServices.MarshalAsAttribute Class

Assembly: Mscorlib.dll
Namespace: System.Runtime.InteropServices
Summary
Indicates how the data should be marshaled between managed and unmanaged code.
C# Syntax:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public sealed class MarshalAsAttribute : Attribute
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, a String could be marshaled to unmanaged code as either a UnmanagedType.LPStr, UnmanagedType.LPWStr, UnmanagedType.LPTStr or a System.Runtime.InteropServices.UnmanagedType.BStr (not supported on the shared source CLI) . By default, the string is marshaled as a System.Runtime.InteropServices.UnmanagedType.BStr (not supported on the shared source CLI) to COM methods. The MarshalAsAttribute attribute can be applied to an individual field or parameter to cause that particular string to be marshaled as a UnmanagedType.LPStr instead of a System.Runtime.InteropServices.UnmanagedType.BStr (not supported on the shared source CLI) . For complete details on using this attribute, see the Data Marshaling Specification.

In most cases, the attribute simply identifies the format of the unmanaged data using the UnmanagedType enumeration, as shown in the following example.

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

Some UnmanagedType enumerations require additional information. For example, additional information is needed when the UnmanagedType is UnmanagedType.LPArray. For a complete description of how to use this attribute, see the Data Type Marshaling Spec.

See also:
System.Runtime.InteropServices Namespace

System.Runtime.InteropServices.MarshalAsAttribute Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(short unmanagedType)

Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.
ctor #2 Overloaded:
.ctor(UnmanagedType unmanagedType)

Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.
Public Fields
ArraySubType The element type of the unmanaged array.
MarshalCookie Provides additional information to a custom marshaler.
MarshalType Specifies the fully qualified name of a custom marshaler.
MarshalTypeRef Implements MarshalAsAttribute.MarshalType as a type.
SizeConst Indicates the number of elements in the fixed length array to import.
SizeParamIndex Indicates which parameter contains the count of array elements, much like size_is in COM, and is zero-based.
Public Properties
TypeId
(inherited from System.Attribute)
Read-only

See base class member description: System.Attribute.TypeId


When implemented in a derived class, gets a unique identifier for this Attribute.
Value Read-only

Gets the UnmanagedType value the data is to be marshaled as.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Attribute)
See base class member description: System.Attribute.GetHashCode


Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IsDefaultAttribute
(inherited from System.Attribute)
See base class member description: System.Attribute.IsDefaultAttribute


When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
Match
(inherited from System.Attribute)
See base class member description: System.Attribute.Match


When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Runtime.InteropServices.MarshalAsAttribute Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.
C# Syntax:
public MarshalAsAttribute(
   short unmanagedType
);
Parameters:

unmanagedType

The UnmanagedType value the data is to be marshaled as.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the MarshalAsAttribute class with the specified UnmanagedType value.
C# Syntax:
public MarshalAsAttribute(
   UnmanagedType unmanagedType
);
Parameters:

unmanagedType

The UnmanagedType value the data is to be marshaled as.

Return to top


Field: ArraySubType
Summary
The element type of the unmanaged array.
C# Syntax:
public UnmanagedType ArraySubType;
Remarks
For example, the ArraySubType for a LPWStr array in COM is UnmanagedType.LPWStr.

Return to top


Field: MarshalCookie
Summary
Provides additional information to a custom marshaler.
C# Syntax:
public string MarshalCookie;
Remarks
This field is optional when using a custom marshaler.

For example, the same marshaler could be used to provide a number of wrappers where the cookie is used to indicate the specific wrapper. The cookie is passed to the GetInstance method of the marshaler.

Return to top


Field: MarshalType
Summary
Specifies the fully qualified name of a custom marshaler.
C# Syntax:
public string MarshalType;
Remarks
This field is required to use a custom marshaler.

Return to top


Field: MarshalTypeRef
Summary
Implements MarshalAsAttribute.MarshalType as a type.
C# Syntax:
public Type MarshalTypeRef;
Remarks
Allows easier usage of MarshalAsAttribute.MarshalType by shortening the syntax from:

[MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "Assembly, NameSpace.TypeName"]

To:

[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(NameSpace.TypeName)]

Return to top


Field: SizeConst
Summary
Indicates the number of elements in the fixed length array to import.
C# Syntax:
public int SizeConst;

Return to top


Field: SizeParamIndex
Summary
Indicates which parameter contains the count of array elements, much like size_is in COM, and is zero-based.
C# Syntax:
public short SizeParamIndex;
Remarks
This field is used when building COM friendly managed objects.SizeParamIndex is only valid on managed methods that are called from COM clients, where one of the parameters is an array. Since the marshaler can't determine the size of an unmanaged array, this information is passed in a separate parameter. This field doesn't have any effect on managed code that calls COM objects.
Example
using System.Runtime.InteropServices;
using SomeNamespace;

namespace SomeNamespace
{
    // Force the layout of your fields to the C style struct layout.
    // Without this, the .NET Framework will reorder your fields.
    [StructLayout(LayoutKind.Sequential)]
    public struct Vertex
    {
    	float	x;
	    float	y;
    	float	z;
    }

    class SomeClass
    {
        // Add [In] or [In, Out] attributes as approppriate.
        // Marshal as a C style array of Vertex, where the second (SizeParamIndex is zero-based)
        //  parameter (size) contains the count of array elements.
        [DllImport ("somelib.dll")]
        public static extern void SomeUnsafeMethod(
                                      [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] Vertex[] data,
                                      long size );

        public void SomeMethod()
        {
            Vertex[] verts = new Vertex[3];
            SomeUnsafeMethod( verts, verts.Length );
        }

    }
}

class Test
{
	public static void Main()
    {
        SomeClass AClass = new SomeClass();

        AClass.SomeMethod();
    }
}

    

Return to top


Property: TypeId (read-only)
Inherited
See base class member description: System.Attribute.TypeId

Summary
When implemented in a derived class, gets a unique identifier for this Attribute.
C# Syntax:
public virtual object TypeId {get;}
Remarks
As implemented, this identifier is merely the Type of the attribute. However, it is intended that the unique identifier be used to identify two attributes of the same type.

Return to top


Property: Value (read-only)
Summary
Gets the UnmanagedType value the data is to be marshaled as.
C# Syntax:
public UnmanagedType Value {get;}

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~MarshalAsAttribute();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Attribute.GetHashCode

Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A 32-bit signed integer hash code.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IsDefaultAttribute()
Inherited
See base class member description: System.Attribute.IsDefaultAttribute

Summary
When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
C# Syntax:
public virtual bool IsDefaultAttribute();
Return Value:
true if this instance is the default attribute for the class; otherwise, false.
Remarks
The default implementation of this class returns false, and must be implemented in the derived class to be useful to that class.

The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.

Return to top


Method: Match(
   object obj
)
Inherited
See base class member description: System.Attribute.Match

Summary
When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
C# Syntax:
public virtual bool Match(
   object obj
);
Parameters:

obj

An Object to compare with this instance of Attribute.

Return Value:
true if this instance equals obj; otherwise, false.
Remarks
This method determines if one Attribute equals another. Its default implementation is the same as Attribute.Equals, which performs a value and reference comparison. Override this method to implement support for attribute values, such as flags or bitfields, that consist of components that are meaningful in themselves. For example, consider an attribute whose value is a binary field divided into a bitfield of flags. Two instances of this attribute have one flag in set in common while all the other flags differ. The Equal method cannot determine that the two instances have the same flag set, but the Match method can.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.