FixedAddressValueTypeAttribute 类

定义

在静态值类型字段的整个生存期内固定其地址。 此类不能被继承。

public ref class FixedAddressValueTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
public sealed class FixedAddressValueTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
[System.Serializable]
public sealed class FixedAddressValueTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
type FixedAddressValueTypeAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
[<System.Serializable>]
type FixedAddressValueTypeAttribute = class
    inherit Attribute
Public NotInheritable Class FixedAddressValueTypeAttribute
Inherits Attribute
继承
FixedAddressValueTypeAttribute
属性

示例

以下示例演示了使用该 FixedAddressValueTypeAttribute 属性在内存中固定静态字段。 它定义一个结构,并初始化两个 Age 类型为静态字段的 Age类。 第二类适用于 FixedAddressValueTypeAttribute 固定字段的地址。 在实例化这两个对象之前和之后进行大量内存分配,并调用垃圾回收器。 该示例的输出显示,尽管第一个 Age 字段的地址在垃圾回收后已更改,但应用的字段 FixedAddressValueTypeAttribute 的地址没有。

using System;
using System.Runtime.CompilerServices;

public struct Age {
   public int years;
   public int months;
}

public class FreeClass
{
   public static Age FreeAge;
   
   public static unsafe IntPtr AddressOfFreeAge()
   { 
      fixed (Age* pointer = &FreeAge) 
      { return (IntPtr) pointer; } 
   }
}

public class FixedClass
{
   [FixedAddressValueType]
   public static Age FixedAge;
   
   public static unsafe IntPtr AddressOfFixedAge()
   { 
      fixed (Age* pointer = &FixedAge) 
      { return (IntPtr) pointer; } 
   }   
}

public class Example
{
   public static void Main()
   {
      AllocateMemory();
      
      // Get addresses of static Age fields.
      IntPtr freePtr1 = FreeClass.AddressOfFreeAge();
      AllocateMemory();
      
      IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();
      AllocateMemory();

      // Garbage collection.
      GC.Collect();
      GC.WaitForPendingFinalizers();
      
      // Get addresses of static Age fields after garbage collection.
      IntPtr freePtr2 = FreeClass.AddressOfFreeAge();
      IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();
        
      // Display addresses before and after garbage collection
      Console.WriteLine("Normal static: {0} -> {1}", freePtr1, freePtr2);
      Console.WriteLine("Pinned static:  {0} -> {1}", fixedPtr1, fixedPtr2);  
   }

   // Allocate memory for 100,000 objects.
   static public void AllocateMemory()
   {
      for (int ctr = 0; ctr <= 100000; ctr++)
      {
         object o = new object();      
      }
   }
}
// The example displays output similar to the following:
//       Normal static: 19932420 -> 19863704
//       Pinned static:  19985508 -> 19985508

注解

使用特性将 FixedAddressValueTypeAttribute 静态值类型标记为在创建时固定。

此属性由Microsoft Visual C++编译器使用。

静态值类型字段创建为装箱对象。 这意味着,执行垃圾回收时,其地址可能会更改。 将此属性应用于静态值类型时,其地址在其生存期内保持不变。

构造函数

FixedAddressValueTypeAttribute()

初始化 FixedAddressValueTypeAttribute 类的新实例。

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于