FixedAddressValueTypeAttribute Classe

Definizione

Imposta l'indirizzo di un campo di tipi valore statici per tutta la durata del campo. La classe non può essere ereditata.

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
Ereditarietà
FixedAddressValueTypeAttribute
Attributi

Esempio

Nell'esempio seguente viene illustrato l'uso dell'attributo FixedAddressValueTypeAttribute per aggiungere un campo statico in memoria. Definisce una Age struttura e inizializza due classi con campi statici di tipo Age. La seconda classe si applica FixedAddressValueTypeAttribute al pin dell'indirizzo del campo. Una serie di allocazioni di memoria vengono eseguite prima e dopo che questi due oggetti vengono creati un'istanza e viene richiamato il Garbage Collector. L'output dell'esempio mostra che, anche se l'indirizzo del primo Age campo è cambiato dopo Garbage Collection, l'indirizzo del campo a cui FixedAddressValueTypeAttribute viene applicato non è stato applicato.

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

Commenti

Usare l'attributo FixedAddressValueTypeAttribute per contrassegnare i tipi di valore statici come aggiunti alla creazione.

Questo attributo viene usato dal compilatore Microsoft Visual C++.

I campi tipo valore statico vengono creati come oggetti boxed. Ciò significa che l'indirizzo può cambiare quando viene eseguita Garbage Collection. Quando si applica questo attributo a un tipo di valore statico, l'indirizzo rimane costante durante la durata.

Costruttori

FixedAddressValueTypeAttribute()

Inizializza una nuova istanza della classe FixedAddressValueTypeAttribute.

Proprietà

TypeId

Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.

(Ereditato da Attribute)

Metodi

Equals(Object)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

(Ereditato da Attribute)
GetHashCode()

Restituisce il codice hash per l'istanza.

(Ereditato da Attribute)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

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

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso a proprietà e metodi esposti da un oggetto.

(Ereditato da Attribute)

Si applica a