PackingSize PackingSize PackingSize PackingSize Enum

定义

指定在封送类型时用于确定字段的内存对齐方式的两个因数中的一个。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
继承
属性

字段

Size1 Size1 Size1 Size1 1

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

Size128 Size128 Size128 Size128 128

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

Size16 Size16 Size16 Size16 16

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

Size2 Size2 Size2 Size2 2

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

Size32 Size32 Size32 Size32 32

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

Size4 Size4 Size4 Size4 4

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

Size64 Size64 Size64 Size64 64

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

Size8 Size8 Size8 Size8 8

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

Unspecified Unspecified Unspecified Unspecified 0

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

注解

封装大小会影响结构和类是其组织中的字段的对齐方式LayoutKind.SequentialPacking 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字段是零 (0) 和的偏移量long字段是一 (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)。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.

适用于