FieldOffsetAttribute 类

指示字段在类或结构的非托管表示形式内的物理位置。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Field, Inherited:=False)> _
Public NotInheritable Class FieldOffsetAttribute
    Inherits Attribute
用法
Dim instance As FieldOffsetAttribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Field, Inherited=false)] 
public sealed class FieldOffsetAttribute : Attribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Field, Inherited=false)] 
public ref class FieldOffsetAttribute sealed : public Attribute
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Field, Inherited=false) */ 
public final class FieldOffsetAttribute extends Attribute
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Field, Inherited=false) 
public final class FieldOffsetAttribute extends Attribute

备注

可将该属性应用于字段。

在以下情况下使用此属性:System.Runtime.InteropServices.StructLayoutAttribute(向其构造函数传递 LayoutKind.Explicit)应用于类或结构,以指定该类或结构的非托管表示形式中每个非 static 成员或常数成员的偏移量。

示例

下面的示例演示了如何将 FieldOffsetAttribute 应用于具有显式布局的类的成员。

<StructLayout(LayoutKind.Explicit)> _
Public Class SYSTEM_INFO
    <FieldOffset(0)> Private OemId As System.UInt64
    <FieldOffset(4)> Private PageSize As System.UInt64
    <FieldOffset(16)> Private ActiveProcessorMask As System.UInt64
    <FieldOffset(20)> Private NumberOfProcessors As System.UInt64
    <FieldOffset(24)> Private ProcessorType As System.UInt64
End Class
[StructLayout(LayoutKind.Explicit)]
public class SYSTEM_INFO
{
[FieldOffset(0)] public ulong OemId;
[FieldOffset(4)] public ulong PageSize;
[FieldOffset(16)] public ulong ActiveProcessorMask;
[FieldOffset(20)] public ulong NumberOfProcessors;
[FieldOffset(24)] public ulong ProcessorType;
}
[StructLayout(LayoutKind::Explicit)]
public ref class SYSTEM_INFO
{
public:

   [FieldOffset(0)]
   UInt64 OemId;

   [FieldOffset(4)]
   UInt64 PageSize;

   [FieldOffset(16)]
   UInt64 ActiveProcessorMask;

   [FieldOffset(20)]
   UInt64 NumberOfProcessors;

   [FieldOffset(24)]
   UInt64 ProcessorType;
};
/** @attribute StructLayout(LayoutKind.Explicit)
 */
public class SYSTEM_INFO
{   
    /** @attribute FieldOffset(0)
     */
    public long oemId;
    
    /** @attribute FieldOffset(4)
     */
    public long pageSize;
    
    /** @attribute FieldOffset(16)
     */
    public long activeProcessorMask;
   
    /** @attribute FieldOffset(20)
     */
    public long numberOfProcessors;
    
    /** @attribute FieldOffset(24)
     */
    public long processorType;
   
} //SYSTEM_INFO

继承层次结构

System.Object
   System.Attribute
    System.Runtime.InteropServices.FieldOffsetAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

FieldOffsetAttribute 成员
System.Runtime.InteropServices 命名空间
StructLayoutAttribute
LayoutKind