Marshal.SizeOf 方法

定義

傳回類別的 Unmanaged 大小 (以位元組為單位)。

多載

SizeOf(Object)
已淘汰.

傳回物件的 Unmanaged 大小 (以位元組為單位)。

SizeOf(Type)
已淘汰.

傳回 Unmanaged 類型的大小 (以位元組為單位)。

SizeOf<T>()

傳回 Unmanaged 類型的大小 (以位元組為單位)。

SizeOf<T>(T)

傳回指定類型之物件的 Unmanaged 大小,以位元組為單位。

SizeOf(Object)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
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

傳回物件的 Unmanaged 大小 (以位元組為單位)。

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

其大小要被傳回的物件。

傳回

Unmanaged 程式碼中指定之物件的大小。

屬性

例外狀況

structure 參數為 null

範例

下列範例會建立 Managed 結構、將它傳輸至 Unmanaged 記憶體,然後將它傳輸回 Managed 記憶體。 這個範例會 SizeOf 使用 方法來判斷要配置的 Unmanaged 記憶體數量。

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 實值型別。 版面配置必須是循序或明確。

傳回的大小是 Unmanaged 物件的大小。 物件的 Unmanaged 和 Managed 大小可能會不同。 對於字元類型,大小會受到 CharSet 套用至該類別的值所影響。

您可以使用 SizeOf 方法來判斷使用 AllocHGlobalAllocCoTaskMem 方法配置多少 Unmanaged 記憶體。

另請參閱

適用於

SizeOf(Type)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
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

傳回 Unmanaged 類型的大小 (以位元組為單位)。

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

要傳回其大小的類型。

傳回

Unmanaged 程式碼中指定之類型的大小。

屬性

例外狀況

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))

備註

當您沒有 結構時,可以使用這個方法。 版面配置必須是循序或明確。

傳回的大小是 Unmanaged 類型的大小。 物件的 Unmanaged 和 Managed 大小可能會不同。 對於字元類型,大小會受到 CharSet 套用至該類別的值所影響。

另請參閱

適用於

SizeOf<T>()

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

傳回 Unmanaged 類型的大小 (以位元組為單位)。

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 泛型類型參數所指定之類型的大小,以位元組為單位。

備註

當您沒有 結構時,可以使用這個方法。 版面配置必須是循序或明確。

傳回的大小是 Unmanaged 類型的大小。 物件的 Unmanaged 和 Managed 大小可能會不同。 對於字元類型,大小會受到 CharSet 套用至該類別的值所影響。

適用於

SizeOf<T>(T)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

傳回指定類型之物件的 Unmanaged 大小,以位元組為單位。

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

其大小要被傳回的物件。

傳回

Unmanaged 程式碼中指定之物件的大小,以位元組為單位。

例外狀況

structure 參數為 null

備註

這個方法接受 結構的實例,它可以是參考型別或 Boxed 實值型別。 版面配置必須是循序或明確。

傳回的大小是 Unmanaged 物件的大小。 物件的 Unmanaged 和 Managed 大小可能會不同。 對於字元類型,大小會受到 CharSet 套用至該類別的值所影響。

您可以使用 SizeOf<T>(T) 方法來判斷使用 AllocHGlobalAllocCoTaskMem 方法配置多少 Unmanaged 記憶體。

適用於