FixedAddressValueTypeAttribute 类    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在静态值类型字段的整个生存期内固定其地址。 此类不能被继承。
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 AttributePublic NotInheritable Class FixedAddressValueTypeAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例演示如何使用 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) |