PackingSize 枚举

定义

指定在封送类型时用于确定字段的内存对齐方式的两个因数中的一个。Specifies one of two factors that determine the memory alignment of fields when a type is marshaled.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class PackingSize
[System.Runtime.InteropServices.ComVisible(true)]
[System.Flags]
[System.Serializable]
public enum PackingSize
type PackingSize = 
Public Enum PackingSize
继承
PackingSize
属性

字段

Size1 1

封装大小为 1 个字节。The packing size is 1 byte.

Size128 128

封装大小为 128 个字节。The packing size is 128 bytes.

Size16 16

封装大小为 16 个字节。The packing size is 16 bytes.

Size2 2

封装大小为 2 个字节。The packing size is 2 bytes.

Size32 32

封装大小为 32 个字节。The packing size is 32 bytes.

Size4 4

封装大小为 4 个字节。The packing size is 4 bytes.

Size64 64

封装大小为 64 个字节。The packing size is 64 bytes.

Size8 8

封装大小为 8 个字节。The packing size is 8 bytes.

Unspecified 0

未指定封装大小。The packing size is not specified.

注解

包装大小会影响结构中的字段和组织LayoutKind.Sequential所属的类的对齐方式。Packing size affects the alignment of fields in structures and classes whose organization is LayoutKind.Sequential. 包装大小是在封送结构或类时确定字段偏移量的两个因素之一;另一个因素是字段的有效大小。The packing size is one of two factors that determine the offset of a field when the structure or class is marshaled; the other factor is the effective size of the field. 确定字段的偏移量:To determine the offset of a field:

  1. 计算偏移量的最小值, 方法是将前面字段的大小添加到前面字段的偏移量。Compute the minimum value for the offset by adding the size of the preceding field to the offset of the preceding field.

  2. 计算字段的自然包装大小:如果该字段是简单数据类型, 请使用大小。Compute the natural packing size for the field: If the field is a simple data type, use the size. 如果该字段为数组, 请使用数组元素的大小。If the field is an array, use the size of an array element. 如果字段本身是结构, 请将该结构的封装大小与该结构中最大字段的大小进行比较, 然后使用这两个值中较小的一个。If the field is itself a structure, compare the packing size of that structure to the size of the largest field in that structure, and use the smaller of the two values.

  3. 通过将封装大小与字段的自然包装大小进行比较 (在步骤2中计算) 来计算有效的封装大小。Compute the effective packing size by comparing the packing size to the natural packing size of the field, computed in step 2. 使用这两个数字中较小的一个。Use the smaller of the two numbers.

  4. 计算偏移量。Compute the offset. 偏移量是等于或大于最小值的第一个内存位置, 并且是在步骤3中计算出的有效包装大小的倍数。The offset is the first memory location that is equal to or larger than the minimum, and a multiple of the effective packing size computed in step 3.

备注

为类或结构中的所有字段计算偏移量后, 将添加填充, 以使整体大小为包装大小的倍数。Once offsets have been computed for all the fields in the class or structure, padding is added so that the overall size is a multiple of the packing size.

byte例如, 在包含和的long类中, 如果使用包装大小Size1, 则byte long字段的偏移量为零 (0), 而字段的偏移量为一 (1)。For example, in a class containing a byte and a long, using packing size Size1, the offset of the byte field is zero (0) and the offset of the long field is one (1). 如果封装大小为, Size4则字段long的偏移量为四 (4)。If the packing size is Size4, the long field has offset four (4). 在包含两个short字段的类中, 使用任意包装大小, 第二short个字段的偏移量为 2 (2)。In a class containing two short fields, using any packing size, the offset of the second short field is two (2).

备注

如果更改封装大小, 则会使字段在不是大小倍数的偏移上对齐。Changing the packing size can affect performance if it causes fields to be aligned on offsets that are not multiples of their size.

适用于