System.Runtime.InteropServices.FieldOffsetAttribute Class

public sealed class FieldOffsetAttribute : Attribute

Base Types

Object
  Attribute
    FieldOffsetAttribute

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

Indicates the physical position of a field within the unmanaged representation of a class or structure.

Description

The target objects for this attribute are non-static fields of classes and structures qualified with the StructLayoutAttribute set to System.Runtime.InteropServices.LayoutKind.Explicit . All non-static fields within an object with an explicit layout are required to have this attribute. No static or constant fields within an object with explicit layout are allowed to have this attribute.

The physical layout of the data members of a class or structure is automatically arranged in managed memory. When a managed object is passed as an argument to unmanaged code, the system creates its unmanaged representation. StructLayoutAttribute provides explicit control over this unmanaged representation. FieldOffsetAttribute indicates the offset of a target data member within the unmanaged representation of a class or structure.

If FieldOffsetAttribute instances on target fields of an exported object are set to overlap each other, one field is overwritten by another field. For example, if an integer field has the FieldOffsetAttribute set to 4, and another integer field has the FieldOffsetAttribute set to 6, the last two bytes of the unmanaged representation of the first integer overlap the first two bytes of the second integer. In such a situation writing to one of the fields might corrupt the data in the other.

[Note: See the StructLayoutAttribute class overview for an example that uses FieldOffsetAttribute.]

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

Attributes

AttributeUsageAttribute(AttributeTargets.Field, AllowMultiple=false, Inherited=false)

See Also

System.Runtime.InteropServices Namespace

Members

FieldOffsetAttribute Constructors

FieldOffsetAttribute Constructor

FieldOffsetAttribute Properties

FieldOffsetAttribute.Value Property


FieldOffsetAttribute Constructor

public FieldOffsetAttribute(int offset);

Summary

Constructs and initializes a new instance of the FieldOffsetAttribute class with the specified field offset in the class or structure.

Parameters

offset
A Int32 that specifies the offset in bytes from the beginning of the structure to the beginning of the field.

Description

If offset is negative, its value is interpreted as an unsigned Int64 value, and the system behavior is implementation-defined.

The System.Runtime.InteropServices.FieldOffsetAttribute.Value property of the new instance is initialized to offset .

See Also

System.Runtime.InteropServices.FieldOffsetAttribute Class, System.Runtime.InteropServices Namespace

FieldOffsetAttribute.Value Property

public int Value { get; }

Summary

Gets the offset from the beginning of the exported data object to the beginning of the target field.

Property Value

A Int32 containing the offset from the beginning of the exported data object to the beginning of the target field.

Description

This property is read-only.

See Also

System.Runtime.InteropServices.FieldOffsetAttribute Class, System.Runtime.InteropServices Namespace