Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Marshal.OffsetOf Method (Type, String)

Note: This API is now obsolete.

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

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

[ObsoleteAttribute("OffsetOf(Type, string) may be unavailable in future releases. Instead, use OffsetOf<T>(string). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296511")]
public static IntPtr OffsetOf(
	Type t,
	string fieldName
)

Parameters

t
Type: System.Type

A value type or formatted reference type that specifies the managed 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 specified class that is declared by platform invoke.

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.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Obsolete (compiler warning) in 4.6

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Obsolete (compiler warning) in Windows Phone 8.1

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft