Dela via


Value type fields should be portable

TypeName

ValueTypeFieldsShouldBePortable

CheckId

CA1900

Category

Microsoft.Portability

Breaking Change

Breaking - If the field is visible outside the assembly.Non Breaking - If the field is not visible outside the assembly.

Cause

This rule checks that structures declared with explicit layout will align correctly when marshaled to unmanaged code on 64-bit platforms. IA-64 does not allow unaligned memory accesses and the process will crash if this violation is not fixed.

Rule Description

Structures with explicit layout that contain misaligned fields cause crashes on 64-bit platforms.

How to Fix Violations

All fields that are smaller than 8 bytes must have offsets that are multiple of their size, and fields that are 8 bytes or greater must have offsets that are a multiple of 8. Another solution is to use LayoutKind.Sequential instead of LayoutKind.Explicit if reasonable.

When to Suppress Warnings

This warning should be suppressed only if it occurs in error.