OffsetOf Method
TOC
Collapse the table of content
Expand the table of content

Marshal.OffsetOf Method

Returns the field offset of the unmanaged form of the managed class.

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static IntPtr OffsetOf(
	Type t,
	string fieldName
)

Parameters

t
Type: System.Type

A Type, specifying the specified class. You must apply the StructLayoutAttribute to the class.

fieldName
Type: System.String

The field within the t parameter.

Return Value

Type: System.IntPtr
The offset, in bytes, for the fieldName parameter within the platform invoke declared class t.

ExceptionCondition
ArgumentException

The class cannot be exported as a structure or the field is nonpublic. Beginning with the .NET Framework version 2.0, the field may be private.

ArgumentNullException

The t parameter is null.

OffsetOf provides the offset in terms of the unmanaged structure layout, which does not necessarily correspond to the offset of the managed structure layout. Marshaling the structure can transform the layout and alter the offset. The t parameter can be a value type or a formatted reference type (with either a sequential or explicit layout). You can obtain the size of the entire layout by using the Marshal.SizeOf method. For additional information, see Default Marshaling for Value Types.

NoteNote:

Beginning with the .NET Framework version 2.0, OffsetOf may expose private fields.

NoteNote:

This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft