Marshal.SizeOf 메서드

정의

클래스의 관리되지 않는 크기(바이트)를 반환합니다.

오버로드

SizeOf(Object)
사용되지 않음.

개체의 관리되지 않는 크기(바이트)를 반환합니다.

SizeOf(Type)
사용되지 않음.

관리되지 않는 형식의 크기(바이트)를 반환합니다.

SizeOf<T>()

관리되지 않는 형식의 크기(바이트)를 반환합니다.

SizeOf<T>(T)

지정된 형식의 개체의 관리되지 않은 크기를 반환합니다(바이트 단위).

SizeOf(Object)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

주의

SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514

개체의 관리되지 않는 크기(바이트)를 반환합니다.

public:
 static int SizeOf(System::Object ^ structure);
[System.Obsolete("SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514")]
public static int SizeOf (object structure);
public static int SizeOf (object structure);
[System.Runtime.InteropServices.ComVisible(true)]
public static int SizeOf (object structure);
[<System.Obsolete("SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514")>]
static member SizeOf : obj -> int
static member SizeOf : obj -> int
[<System.Runtime.InteropServices.ComVisible(true)>]
static member SizeOf : obj -> int
Public Shared Function SizeOf (structure As Object) As Integer

매개 변수

structure
Object

크기가 반환되는 개체입니다.

반환

비관리 코드에서 지정된 개체의 크기입니다.

특성

예외

structure 매개 변수가 null인 경우

예제

다음 예제에서는 관리되는 구조를 만들고 관리되지 않는 메모리로 전송한 다음 관리되는 메모리로 다시 전송합니다. 이 예제에서는 메서드를 SizeOf 사용하여 할당할 관리되지 않는 메모리의 양을 결정합니다.

using System;
using System.Runtime.InteropServices;

public struct Point
{
    public int x;
    public int y;
}

class Example
{

    static void Main()
    {

        // Create a point struct.
        Point p;
        p.x = 1;
        p.y = 1;

        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");

        // Initialize unmanged memory to hold the struct.
        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));

        try
        {

            // Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, false);

            // Create another point.
            Point anotherP;

            // Set this Point to the value of the
            // Point in unmanaged memory.
            anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));

            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}
Imports System.Runtime.InteropServices



Public Structure Point
    Public x As Integer
    Public y As Integer
End Structure


Module Example


    Sub Main()

        ' Create a point struct.
        Dim p As Point
        p.x = 1
        p.y = 1

        Console.WriteLine("The value of first point is " + p.x.ToString + " and " + p.y.ToString + ".")

        ' Initialize unmanged memory to hold the struct.
        Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p))

        Try

            ' Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, False)

            ' Create another point.
            Dim anotherP As Point

            ' Set this Point to the value of the 
            ' Point in unmanaged memory. 
            anotherP = CType(Marshal.PtrToStructure(pnt, GetType(Point)), Point)

            Console.WriteLine("The value of new point is " + anotherP.x.ToString + " and " + anotherP.y.ToString + ".")

        Finally
            ' Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt)
        End Try

    End Sub
End Module

설명

이 메서드는 참조 형식 또는 boxed 값 형식일 수 있는 구조체의 instance 허용합니다. 레이아웃은 순차적이거나 명시적이어야 합니다.

반환되는 크기는 관리되지 않는 개체의 크기입니다. 개체의 관리되지 않는 크기 및 관리되는 크기는 다를 수 있습니다. 문자 형식의 경우 크기는 해당 클래스에 적용된 값의 CharSet 영향을 받습니다.

메서드를 SizeOf 사용하여 및 AllocCoTaskMem 메서드를 사용하여 AllocHGlobal 할당할 관리되지 않는 메모리의 양을 확인할 수 있습니다.

추가 정보

적용 대상

SizeOf(Type)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

주의

SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515

관리되지 않는 형식의 크기(바이트)를 반환합니다.

public:
 static int SizeOf(Type ^ t);
[System.Obsolete("SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515")]
public static int SizeOf (Type t);
public static int SizeOf (Type t);
[<System.Obsolete("SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515")>]
static member SizeOf : Type -> int
static member SizeOf : Type -> int
Public Shared Function SizeOf (t As Type) As Integer

매개 변수

t
Type

크기가 반환되는 형식입니다.

반환

비관리 코드에서 지정된 형식의 크기입니다.

특성

예외

t 매개 변수는 제네릭 형식 정의입니다.

t 매개 변수가 null인 경우

예제

다음 예제에서는 SizeOf 메서드를 호출하는 방법을 보여 줍니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 Marshal 클래스입니다.

// Demonstrate the use of the SizeOf method of the Marshal
// class.
Console::WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal::SizeOf(Point::typeid));
Point point;
Console::WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal::SizeOf(point));
// Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal.SizeOf(typeof(Point)));
Point p = new Point();
Console.WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal.SizeOf(p));
' Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(GetType(Point)))
Dim p As New Point()
Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(p))

설명

구조체가 없는 경우 이 메서드를 사용할 수 있습니다. 레이아웃은 순차적이거나 명시적이어야 합니다.

반환되는 크기는 관리되지 않는 형식의 크기입니다. 개체의 관리되지 않는 크기 및 관리되는 크기는 다를 수 있습니다. 문자 형식의 경우 크기는 해당 클래스에 적용된 값의 CharSet 영향을 받습니다.

추가 정보

적용 대상

SizeOf<T>()

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

관리되지 않는 형식의 크기(바이트)를 반환합니다.

public:
generic <typename T>
 static int SizeOf();
public static int SizeOf<T> ();
static member SizeOf : unit -> int
Public Shared Function SizeOf(Of T) () As Integer

형식 매개 변수

T

크기가 반환되는 형식입니다.

반환

T 제네릭 형식 매개 변수로 지정된 형식의 크기(바이트 단위)입니다.

설명

구조체가 없는 경우 이 메서드를 사용할 수 있습니다. 레이아웃은 순차적이거나 명시적이어야 합니다.

반환되는 크기는 관리되지 않는 형식의 크기입니다. 개체의 관리되지 않는 크기 및 관리되는 크기는 다를 수 있습니다. 문자 형식의 경우 크기는 해당 클래스에 적용된 값의 CharSet 영향을 받습니다.

적용 대상

SizeOf<T>(T)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

지정된 형식의 개체의 관리되지 않은 크기를 반환합니다(바이트 단위).

public:
generic <typename T>
 static int SizeOf(T structure);
public static int SizeOf<T> (T structure);
static member SizeOf : 'T -> int
Public Shared Function SizeOf(Of T) (structure As T) As Integer

형식 매개 변수

T

structure 매개 변수의 형식입니다.

매개 변수

structure
T

크기가 반환되는 개체입니다.

반환

비관리 코드에서 지정된 개체의 크기(바이트)입니다.

예외

structure 매개 변수가 null인 경우

설명

이 메서드는 참조 형식 또는 boxed 값 형식일 수 있는 구조체의 instance 허용합니다. 레이아웃은 순차적이거나 명시적이어야 합니다.

반환되는 크기는 관리되지 않는 개체의 크기입니다. 개체의 관리되지 않는 크기 및 관리되는 크기는 다를 수 있습니다. 문자 형식의 경우 크기는 해당 클래스에 적용된 값의 CharSet 영향을 받습니다.

메서드를 SizeOf<T>(T) 사용하여 및 AllocCoTaskMem 메서드를 사용하여 AllocHGlobal 할당할 관리되지 않는 메모리의 양을 확인할 수 있습니다.

적용 대상