LayoutKind 열거형
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
비관리 코드로 내보낼 때 개체의 레이아웃을 제어합니다.
public enum class LayoutKind
public enum LayoutKind
[System.Serializable]
public enum LayoutKind
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum LayoutKind
type LayoutKind =
[<System.Serializable>]
type LayoutKind =
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type LayoutKind =
Public Enum LayoutKind
- 상속
- 특성
필드
Auto | 3 | 런타임에서는 자동으로 관리되지 않는 메모리에 있는 개체의 멤버에 적합한 레이아웃을 선택합니다. 이 열거형 멤버로 정의된 개체는 관리 코드 외부에 노출시킬 수 없습니다. 관리 코드 외부에 노출시키려고 하면 예외가 발생합니다. |
Explicit | 2 | 관리되지 않는 메모리에 있는 개체의 각 멤버에 대한 정확한 위치는 명시적으로 제어됩니다. 이 위치는 Pack 필드의 설정을 기준으로 합니다. 각 멤버는 FieldOffsetAttribute를 사용하여 형식 내부에서 필드의 위치를 나타내야 합니다. |
Sequential | 0 | 개체의 멤버는 관리되지 않는 메모리로 내보낼 때 표시되는 순서대로 배치됩니다. 멤버는 Pack에서 지정된 방식에 따라 배치되며, 연속되지 않을 수 있습니다. |
예제
다음 예제에서는 사각형 내에 점이 있는지 여부를 확인하고 순차 레이아웃과 명시적 레이아웃이 있는 구조체를 정의하는 Point
함수의 관리되는 선언 PtInRect
을 Rect
보여 줍니다.
enum class Bool
{
False = 0,
True
};
[StructLayout(LayoutKind::Sequential)]
value struct Point
{
public:
int x;
int y;
};
[StructLayout(LayoutKind::Explicit)]
value struct Rect
{
public:
[FieldOffset(0)]
int left;
[FieldOffset(4)]
int top;
[FieldOffset(8)]
int right;
[FieldOffset(12)]
int bottom;
};
ref class NativeMethods
{
public:
[DllImport("user32.dll",CallingConvention=CallingConvention::StdCall)]
static Bool PtInRect( Rect * r, Point p );
};
int main()
{
try
{
Bool bPointInRect = (Bool)0;
Rect myRect = Rect( );
myRect.left = 10;
myRect.right = 100;
myRect.top = 10;
myRect.bottom = 100;
Point myPoint = Point( );
myPoint.x = 50;
myPoint.y = 50;
bPointInRect = NativeMethods::PtInRect( &myRect, myPoint );
if ( bPointInRect == Bool::True )
Console::WriteLine( "Point lies within the Rect" );
else
Console::WriteLine( "Point did not lie within the Rect" );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
enum Bool
{
False = 0,
True
};
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
}
internal static class NativeMethods
{
[DllImport("user32.dll", CallingConvention=CallingConvention.StdCall)]
internal static extern Bool PtInRect(ref Rect r, Point p);
};
class TestApplication
{
public static void Main()
{
try
{
Bool bPointInRect = 0;
Rect myRect = new Rect();
myRect.left = 10;
myRect.right = 100;
myRect.top = 10;
myRect.bottom = 100;
Point myPoint = new Point();
myPoint.x = 50;
myPoint.y = 50;
bPointInRect = NativeMethods.PtInRect(ref myRect, myPoint);
if(bPointInRect == Bool.True)
Console.WriteLine("Point lies within the Rect");
else
Console.WriteLine("Point did not lie within the Rect");
}
catch(Exception e)
{
Console.WriteLine("Exception : " + e.Message);
}
}
}
' The program shows a managed declaration of the PtInRect function and defines Point
' structure with sequential layout and Rect structure with explicit layout. The PtInRect
' checks the point lies within the rectangle or not.
Imports System.Runtime.InteropServices
Enum Bool
[False] = 0
[True]
End Enum
<StructLayout(LayoutKind.Sequential)> _
Public Structure Point
Public x As Integer
Public y As Integer
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure Rect
<FieldOffset(0)> Public left As Integer
<FieldOffset(4)> Public top As Integer
<FieldOffset(8)> Public right As Integer
<FieldOffset(12)> Public bottom As Integer
End Structure
Friend Class NativeMethods
<DllImport("user32.dll", CallingConvention := CallingConvention.StdCall)> _
Friend Shared Function PtInRect(ByRef r As Rect, p As Point) As Bool
End Function
End Class
Class TestApplication
Public Shared Sub Main()
Try
Dim bPointInRect As Bool = 0
Dim myRect As New Rect()
myRect.left = 10
myRect.right = 100
myRect.top = 10
myRect.bottom = 100
Dim myPoint As New Point()
myPoint.x = 50
myPoint.y = 50
bPointInRect = NativeMethods.PtInRect(myRect, myPoint)
If bPointInRect = Bool.True Then
Console.WriteLine("Point lies within the Rect")
Else
Console.WriteLine("Point did not lie within the Rect")
End If
Catch e As Exception
Console.WriteLine(("Exception : " + e.Message.ToString()))
End Try
End Sub
End Class
설명
이 열거형은 와 함께 StructLayoutAttribute사용됩니다. 공용 언어 런타임은 기본적으로 레이아웃 값을 사용합니다 Auto
. 값과 Auto
관련된 레이아웃 관련 문제를 줄이기 위해 C#, Visual Basic 및 C++ 컴파일러에서 값 형식에 대한 레이아웃을 지정 Sequential
합니다.
중요
StructLayoutAttribute.Pack 필드는 데이터 필드의 맞춤을 제어하므로 지정한 값에 관계없이 레이아웃에 LayoutKind 영향을 줍니다. 기본적으로 값 Pack 은 0이며 현재 플랫폼의 기본 압축 크기를 나타냅니다. 예를 들어 레이아웃 값을 사용하고 Explicit
바이트 경계에서 필드 맞춤을 지정하는 경우 원하는 결과를 얻으려면 1로 설정 Pack 해야 합니다.
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기