FixedAddressValueTypeAttribute 类

定义

修复了静态值类型字段在其生存期内的地址。 此类不能被继承。

public ref class FixedAddressValueTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
[System.Serializable]
public sealed class FixedAddressValueTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
public sealed class FixedAddressValueTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
[<System.Serializable>]
type FixedAddressValueTypeAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
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)

适用于