Bewerken

Share via


Marshal.OffsetOf Method

Definition

Overloads

OffsetOf(Type, String)
Obsolete.

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

OffsetOf<T>(String)

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

OffsetOf(Type, String)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

Caution

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

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

public:
 static IntPtr OffsetOf(Type ^ t, System::String ^ fieldName);
[System.Obsolete("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);
public static IntPtr OffsetOf (Type t, string fieldName);
[<System.Obsolete("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")>]
static member OffsetOf : Type * string -> nativeint
static member OffsetOf : Type * string -> nativeint
Public Shared Function OffsetOf (t As Type, fieldName As String) As IntPtr

Parameters

t
Type

A value type or formatted reference type that specifies the managed class. You must apply the StructLayoutAttribute to the class.

fieldName
String

The field within the t parameter.

Returns

IntPtr

nativeint

The offset, in bytes, for the fieldName parameter within the specified class that is declared by platform invoke.

Attributes

Exceptions

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.

The t parameter is null.

Remarks

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.

Note

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

See also

Applies to

OffsetOf<T>(String)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

public:
generic <typename T>
 static IntPtr OffsetOf(System::String ^ fieldName);
public static IntPtr OffsetOf<T> (string fieldName);
static member OffsetOf : string -> nativeint
Public Shared Function OffsetOf(Of T) (fieldName As String) As IntPtr

Type Parameters

T

A managed value type or formatted reference type. You must apply the StructLayoutAttribute attribute to the class.

Parameters

fieldName
String

The name of the field in the T type.

Returns

IntPtr

nativeint

The offset, in bytes, for the fieldName parameter within the specified class that is declared by platform invoke.

Remarks

OffsetOf<T>(String) 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 generic type 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 SizeOf<T>(T) method. For additional information, see Default Marshaling for Value Types.

Note

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

See also

Applies to