Marshal.SizeOf Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Devuelve el tamaño no administrado, en bytes, de una clase.
Sobrecargas
| SizeOf(Object) |
Obsoletos.
Devuelve el tamaño no administrado de un objeto en bytes. |
| SizeOf(Type) |
Obsoletos.
Devuelve el tamaño, expresado en bytes, de un tipo no administrado. |
| SizeOf<T>() |
Devuelve el tamaño, expresado en bytes, de un tipo no administrado. |
| SizeOf<T>(T) |
Devuelve el tamaño no administrado de un objeto de un tipo especificado en bytes. |
SizeOf(Object)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
Precaución
SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514
Devuelve el tamaño no administrado de un objeto en bytes.
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
Parámetros
- structure
- Object
Objeto cuyo tamaño se devolverá.
Devoluciones
Tamaño del objeto especificado en código no administrado.
- Atributos
Excepciones
El parámetro structure es null.
Ejemplos
En el ejemplo siguiente se crea una estructura administrada, se transfiere a la memoria no administrada y, a continuación, se transfiere a la memoria administrada. En este ejemplo se usa el SizeOf método para determinar la cantidad de memoria no administrada que se va a asignar.
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
Comentarios
Este método acepta una instancia de una estructura, que puede ser un tipo de referencia o un tipo de valor con conversión boxing. El diseño debe ser secuencial o explícito.
El tamaño devuelto es el tamaño del objeto no administrado. Los tamaños no administrados y administrados de un objeto pueden diferir. En el caso de los tipos de caracteres, el tamaño se ve afectado por el CharSet valor aplicado a esa clase.
Puede usar el SizeOf método para determinar la cantidad de memoria no administrada que se va a asignar mediante los AllocHGlobal métodos y AllocCoTaskMem .
Consulte también
Se aplica a
SizeOf(Type)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
Precaución
SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515
Devuelve el tamaño, expresado en bytes, de un tipo no administrado.
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
Parámetros
- t
- Type
Tipo cuyo tamaño se va a devolver.
Devoluciones
Tamaño del tipo especificado en código no administrado.
- Atributos
Excepciones
El parámetro t es una definición de tipo genérico.
El parámetro t es null.
Ejemplos
En el ejemplo siguiente se muestra la forma de llamar al método SizeOf. Este ejemplo de código es parte de un ejemplo más grande proporcionado para la clase 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))
Comentarios
Puede usar este método cuando no tenga una estructura. El diseño debe ser secuencial o explícito.
El tamaño devuelto es el tamaño del tipo no administrado. Los tamaños no administrados y administrados de un objeto pueden diferir. En el caso de los tipos de caracteres, el tamaño se ve afectado por el CharSet valor aplicado a esa clase.
Consulte también
Se aplica a
SizeOf<T>()
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
Devuelve el tamaño, expresado en bytes, de un tipo no administrado.
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
Parámetros de tipo
- T
Tipo cuyo tamaño se va a devolver.
Devoluciones
El tamaño, en bytes, del tipo especificado por el parámetro de tipo genérico T.
Comentarios
Puede usar este método cuando no tenga una estructura. El diseño debe ser secuencial o explícito.
El tamaño devuelto es el tamaño del tipo no administrado. Los tamaños no administrados y administrados de un objeto pueden diferir. En el caso de los tipos de caracteres, el tamaño se ve afectado por el CharSet valor aplicado a esa clase.
Se aplica a
SizeOf<T>(T)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
Devuelve el tamaño no administrado de un objeto de un tipo especificado en bytes.
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
Parámetros de tipo
- T
Tipo del parámetro structure.
Parámetros
- structure
- T
Objeto cuyo tamaño se devolverá.
Devoluciones
Tamaño del objeto especificado, en bytes, en código no administrado.
Excepciones
El parámetro structure es null.
Comentarios
Este método acepta una instancia de una estructura, que puede ser un tipo de referencia o un tipo de valor con conversión boxing. El diseño debe ser secuencial o explícito.
El tamaño devuelto es el tamaño del objeto no administrado. Los tamaños no administrados y administrados de un objeto pueden diferir. En el caso de los tipos de caracteres, el tamaño se ve afectado por el CharSet valor aplicado a esa clase.
Puede usar el SizeOf<T>(T) método para determinar la cantidad de memoria no administrada que se va a asignar mediante los AllocHGlobal métodos y AllocCoTaskMem .