This documentation is archived and is not being maintained.

Marshal.OffsetOf Method

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

[Visual Basic]
Public Shared Function OffsetOf( _
   ByVal t As Type, _
   ByVal fieldName As String _
) As IntPtr
public static IntPtr OffsetOf(
 Type t,
 string fieldName
public: static IntPtr OffsetOf(
 Type* t,
 String* fieldName
public static function OffsetOf(
   t : Type,
 fieldName : String
) : IntPtr;


A Type, specifying the specified class. You must apply the StructLayoutAttribute to the class.
The field within the t parameter.

Return Value

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


Exception Type Condition
ArgumentException The class cannot be exported as a structure or the field is nonpublic.


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 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   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.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace | SizeOf