MarshalAsAttribute 클래스

정의

관리 코드와 비관리 코드 간에 데이터를 마샬링하는 방법을 나타냅니다.

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
상속
MarshalAsAttribute
특성

예제

다음 예제에서는 필드, 메서드 매개 변수 및 관리되는 형식의 메서드 반환 값에 를 적용 MarshalAsAttribute 합니다.

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

다음 예제에서는 속성을 속성에 적용합니다 MarshalAsAttribute .

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

설명

매개 변수, 필드 또는 반환 값에 이 특성을 적용할 수 있습니다.

각 데이터 형식에 기본 마샬링 동작이 있으므로 이 특성은 선택 사항입니다. 이 특성은 지정된 형식을 여러 형식으로 마샬링할 수 있는 경우에만 필요합니다. 예를 들어 문자열을 관리되지 않는 코드에 , , LPWStr또는 LPTStrBStrLPStr마샬링할 수 있습니다. 기본적으로 공용 언어 런타임은 문자열 매개 변수를 COM 메서드로 BStr 마샬링합니다. 특성을 개별 필드 또는 매개 변수에 적용 MarshalAsAttribute 하여 특정 문자열이 대신 BStrLPStr 마샬링되도록 할 수 있습니다. Tlbexp.exe(형식 라이브러리 내보내기)는 마샬링 기본 설정을 공용 언어 런타임에 전달합니다.

일부 매개 변수 및 반환 값은 COM interop 또는 플랫폼 호출과 함께 사용할 때 다른 기본 마샬링 동작을 갖습니다. 기본적으로 런타임은 문자열 매개 변수(및 값 형식의 필드)를 플랫폼 호출 메서드 또는 함수에 대한 로 LPStr 마샬링합니다. 자세한 내용은 기본 마샬링 동작을 참조하세요.

대부분의 경우 특성은 다음 C# 서명과 같이 열거형을 사용하여 UnmanagedType 관리되지 않는 데이터의 형식을 식별합니다.

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

일부 UnmanagedType 열거형 멤버에는 추가 정보가 필요합니다. 예를 들어 가 인 경우 UnmanagedTypeLPArray추가 정보가 필요합니다. 배열에서 이 특성을 사용하는 방법에 대한 전체 설명은 배열 에 대한 기본 마샬링을 참조하세요.

또한Tlbimp.exe(형식 라이브러리 가져오기)는 이 특성을 매개 변수, 필드 및 반환 값에 적용하여 입력 형식 라이브러리의 데이터 형식이 해당 관리되는 데이터 형식의 기본 형식이 아님을 나타냅니다. Tlbimp.exe 입력 형식 라이브러리에 지정된 형식에 관계없이 항상 및 Object 형식에 를 적용 MarshalAsAttributeString 합니다.

참고

MarshalAsAttribute 제네릭 형식의 마샬링을 지원하지 않습니다.

생성자

MarshalAsAttribute(Int16)

지정된 MarshalAsAttribute 값을 사용하여 UnmanagedType 클래스의 새 인스턴스를 초기화합니다.

MarshalAsAttribute(UnmanagedType)

지정된 MarshalAsAttribute 열거형 멤버를 사용하여 UnmanagedType 클래스의 새 인스턴스를 초기화합니다.

필드

ArraySubType

관리되지 않는 LPArray 또는 ByValArray의 요소 형식을 지정합니다.

IidParameterIndex

COM에서 사용하는 관리되지 않는 iid_is 특성의 매개 변수 인덱스를 지정합니다.

MarshalCookie

사용자 지정 마샬러에 추가 정보를 제공합니다.

MarshalType

사용자 지정 마샬러의 정규화된 이름을 지정합니다.

MarshalTypeRef

MarshalType을 형식으로 구현합니다.

SafeArraySubType

SafeArray의 요소 형식을 나타냅니다.

SafeArrayUserDefinedSubType

SafeArray의 사용자 정의 요소 형식을 나타냅니다.

SizeConst

가져올 고정 길이 배열의 요소 개수 또는 문자열의 문자 수(바이트 수가 아님)를 나타냅니다.

SizeParamIndex

COM에 있는 size_is와 같이 배열 요소의 수가 들어 있고 0부터 시작하는 매개 변수를 나타냅니다.

속성

TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)
Value

데이터를 마샬링할 UnmanagedType 값을 가져옵니다.

메서드

Equals(Object)

이 인스턴스가 지정된 개체와 같은지를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의된 경우 이 인스턴스 값이 파생 클래스에 대한 기본값인지 여부를 표시합니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의된 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

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

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상

추가 정보