StructLayoutAttribute 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
메모리에 있는 클래스 또는 구조체의 데이터 필드에 대한 실제 레이아웃을 제어할 수 있습니다.
public ref class StructLayoutAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)]
public sealed class StructLayoutAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StructLayoutAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)>]
type StructLayoutAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StructLayoutAttribute = class
inherit Attribute
Public NotInheritable Class StructLayoutAttribute
Inherits Attribute
- 상속
- 특성
예제
다음 예제에서는 함수에 대 한 관리 되는 선언을 보여 주고 GetSystemTime
MySystemTime
레이아웃을 사용 하 여 클래스를 정의 합니다 LayoutKind.Explicit . GetSystemTime
시스템 시간을 가져오고 콘솔에 출력 합니다.
using namespace System;
using namespace System::Runtime::InteropServices;
[StructLayout(LayoutKind::Explicit,Size=16,CharSet=CharSet::Ansi)]
value class MySystemTime
{
public:
[FieldOffset(0)]
short wYear;
[FieldOffset(2)]
short wMonth;
[FieldOffset(4)]
short wDayOfWeek;
[FieldOffset(6)]
short wDay;
[FieldOffset(8)]
short wHour;
[FieldOffset(10)]
short wMinute;
[FieldOffset(12)]
short wSecond;
[FieldOffset(14)]
short wMilliseconds;
};
ref class NativeMethods
{
public:
[DllImport("kernel32.dll")]
static void GetSystemTime( MySystemTime * st );
};
int main()
{
try
{
MySystemTime sysTime;
NativeMethods::GetSystemTime( &sysTime );
Console::WriteLine( "The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond );
}
catch ( TypeLoadException^ e )
{
Console::WriteLine( "TypeLoadException : {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
using System;
using System.Runtime.InteropServices;
namespace InteropSample
{
[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
public class MySystemTime
{
[FieldOffset(0)]public ushort wYear;
[FieldOffset(2)]public ushort wMonth;
[FieldOffset(4)]public ushort wDayOfWeek;
[FieldOffset(6)]public ushort wDay;
[FieldOffset(8)]public ushort wHour;
[FieldOffset(10)]public ushort wMinute;
[FieldOffset(12)]public ushort wSecond;
[FieldOffset(14)]public ushort wMilliseconds;
}
internal static class NativeMethods
{
[DllImport("kernel32.dll")]
internal static extern void GetSystemTime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st);
};
class TestApplication
{
public static void Main()
{
try
{
MySystemTime sysTime = new MySystemTime();
NativeMethods.GetSystemTime(sysTime);
Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay,
sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
}
catch(TypeLoadException e)
{
Console.WriteLine("TypeLoadException : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception : " + e.Message);
}
}
}
}
Imports System.Runtime.InteropServices
Namespace InteropSample
<StructLayout(LayoutKind.Explicit, Size:=16, CharSet:=CharSet.Ansi)> _
Public Class MySystemTime
<FieldOffset(0)> Public wYear As Short
<FieldOffset(2)> Public wMonth As Short
<FieldOffset(4)> Public wDayOfWeek As Short
<FieldOffset(6)> Public wDay As Short
<FieldOffset(8)> Public wHour As Short
<FieldOffset(10)> Public wMinute As Short
<FieldOffset(12)> Public wSecond As Short
<FieldOffset(14)> Public wMilliseconds As Short
End Class
Friend Class NativeMethods
<DllImport("kernel32.dll")> _
Friend Shared Sub GetSystemTime(<MarshalAs(UnmanagedType.LPStruct)> ByVal st As MySystemTime)
End Sub
End Class
Class TestApplication
Public Shared Sub Main()
Try
Dim sysTime As New MySystemTime()
NativeMethods.GetSystemTime(sysTime)
Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond)
Catch e As TypeLoadException
Console.WriteLine(("TypeLoadException : " + e.Message.ToString()))
Catch e As Exception
Console.WriteLine(("Exception : " + e.Message.ToString()))
End Try
End Sub
End Class
End Namespace 'InteropSample
설명
클래스 또는 구조체에이 특성을 적용할 수 있습니다.
공용 언어 런타임은 관리 되는 메모리에 있는 클래스 또는 구조체의 데이터 필드에 대 한 물리적 레이아웃을 제어 합니다. 그러나 형식을 비관리 코드에 전달 하려는 경우 특성을 사용 StructLayoutAttribute 하 여 형식의 관리 되지 않는 레이아웃을 제어할 수 있습니다. 에서 특성을 사용 LayoutKind.Sequential 하 여 멤버를 표시 되는 순서 대로 순차적으로 배치 하도록 강제 합니다. Blittable 형식의 경우 LayoutKind.Sequential 는 관리 되는 메모리의 레이아웃과 관리 되지 않는 메모리의 레이아웃을 제어 합니다. 비 blittable 형식의 경우 클래스 또는 구조체가 비관리 코드로 마샬링될 때 레이아웃을 제어 하지만 관리 되는 메모리에서는 레이아웃을 제어 하지 않습니다. 특성을 사용 LayoutKind.Explicit 하 여 각 데이터 멤버의 정확한 위치를 제어할 수 있습니다. 이는 blittable 형식 및 비 blittable 형식 모두에 대해 관리 되는 레이아웃과 관리 되지 않는 레이아웃에 모두 영향을 줍니다. 를 사용 하려면 특성을 사용 하 여 LayoutKind.Explicit FieldOffsetAttribute 형식 내에서 각 필드의 위치를 나타내야 합니다.
c #, Visual Basic 및 c + + 컴파일러는 Sequential 기본적으로 구조체에 레이아웃 값을 적용 합니다. 클래스의 경우 값을 명시적으로 적용 해야 합니다 LayoutKind.Sequential . Tlbimp.exe (형식 라이브러리 가져오기) 도 특성을 적용 합니다. StructLayoutAttribute LayoutKind.Sequential 형식 라이브러리를 가져올 때 항상 값을 적용 합니다.
생성자
StructLayoutAttribute(Int16) |
지정된 StructLayoutAttribute 열거형 멤버를 사용하여 LayoutKind 클래스의 새 인스턴스를 초기화합니다. |
StructLayoutAttribute(LayoutKind) |
지정된 StructLayoutAttribute 열거형 멤버를 사용하여 LayoutKind 클래스의 새 인스턴스를 초기화합니다. |
필드
CharSet |
클래스 내의 문자열 데이터 필드가 기본적으로 |
Pack |
메모리에 있는 클래스 또는 구조체의 데이터 필드 맞춤을 제어합니다. |
Size |
클래스나 구조체의 절대적인 크기를 나타냅니다. |
속성
TypeId |
파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다. (다음에서 상속됨 Attribute) |
Value |
클래스나 구조의 배열 방법을 지정하는 LayoutKind 값을 가져옵니다. |
메서드
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) |