다음을 통해 공유


MarshalAsAttribute 클래스

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

네임스페이스: System.Runtime.InteropServices
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<AttributeUsageAttribute(AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class MarshalAsAttribute
    Inherits Attribute
‘사용 방법
Dim instance As MarshalAsAttribute
[AttributeUsageAttribute(AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, Inherited=false)] 
[ComVisibleAttribute(true)] 
public sealed class MarshalAsAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, Inherited=false)] 
[ComVisibleAttribute(true)] 
public ref class MarshalAsAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class MarshalAsAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, Inherited=false) 
ComVisibleAttribute(true) 
public final class MarshalAsAttribute extends Attribute

설명

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

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

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

대부분의 경우 이 특성은 다음 C# 시그니처와 같이 UnmanagedType 열거형을 사용하는 관리되지 않는 데이터의 형식을 쉽게 식별합니다.

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

일부 UnmanagedType 열거형 멤버에는 추가 정보가 필요합니다. 예를 들어, UnmanagedTypeLPArray일 때에는 추가 정보가 필요합니다. 이 특성을 배열에 사용하는 방법에 대한 자세한 내용은 배열에 대한 기본 마샬링을 참조하십시오.

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

참고

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

예제

다음 예제에서는 매개 변수, 필드 및 반환 값에 적용되는 경우 관리되는 소스 코드의 MarshalAsAttribute 배치를 보여 줍니다.

Imports System.Runtime.InteropServices

Module Program
    'Applied to a parameter.
    Public Sub M1(<MarshalAs(UnmanagedType.LPWStr)> ByVal msg As String)
    End Sub

    '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
    End Function
End Module

//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();

상속 계층 구조

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

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

MarshalAsAttribute 멤버
System.Runtime.InteropServices 네임스페이스
UnmanagedType

기타 리소스

형식 라이브러리 내보내기(Tlbexp.exe)
형식 라이브러리 가져오기(Tlbimp.exe)