FixedAddressValueTypeAttribute 类
定义
在静态值类型字段的整个生存期内固定其地址。Fixes the address of a static value type field throughout its lifetime. 此类不能被继承。This class cannot be inherited.
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 如何使用属性在内存中固定静态字段。The following example illustrates the use of the FixedAddressValueTypeAttribute attribute to pin a static field in memory. 它定义了一个 Age 结构,并初始化了两个具有类型的静态字段的类 Age 。It defines an Age structure and initializes two classes that have static fields of type Age. 第二个类适用于 FixedAddressValueTypeAttribute 固定字段地址。The second class applies FixedAddressValueTypeAttribute to pin the field's address. 在实例化两个对象并调用垃圾回收器之前和之后,将进行大量的内存分配。A number of memory allocations are made before and after these two objects are instantiated and the garbage collector is invoked. 该示例的输出显示:尽管第一个字段的地址在 Age 垃圾回收后发生了更改,但应用的字段地址却 FixedAddressValueTypeAttribute 没有。The output from the example shows that although the address of the first Age field has changed after garbage collection, the address of the field to which FixedAddressValueTypeAttribute is applied has not.
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 特性可在创建时将静态值类型标记为固定。Use the FixedAddressValueTypeAttribute attribute to mark static value types as pinned upon creation.
此属性由 Microsoft Visual C++ 编译器使用。This attribute is used by the Microsoft Visual C++ compiler.
静态值类型字段作为装箱对象创建。Static value type fields are created as boxed objects. 这意味着在执行垃圾回收时,它们的地址可能会更改。This means that their address can change as garbage collection is performed. 将此特性应用于静态值类型时,其地址在其生存期内保持不变。When you apply this attribute to a static value type, its address remains constant during its lifetime.
构造函数
| FixedAddressValueTypeAttribute() |
初始化 FixedAddressValueTypeAttribute 类的新实例。Initializes a new instance of the FixedAddressValueTypeAttribute class. |
属性
| TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute. (继承自 Attribute) |
方法
| Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object. (继承自 Attribute) |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. (继承自 Attribute) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |