Unsafe.As Metodo

Definizione

Overload

As<T>(Object)

Eseguire il cast dell'oggetto specificato al tipo specificato.

As<TFrom,TTo>(TFrom)

Reinterpreta il puntatore gestito specificato come nuovo puntatore gestito a un valore di tipo TTo.

As<T>(Object)

Origine:
Unsafe.cs
Origine:
Unsafe.cs
Origine:
Unsafe.cs

Eseguire il cast dell'oggetto specificato al tipo specificato.

public:
generic <typename T>
 where T : class static T As(System::Object ^ o);
public static T As<T> (object o) where T : class;
public static T? As<T> (object? o) where T : class;
static member As : obj -> 'T (requires 'T : null)
Public Shared Function As(Of T As Class) (o As Object) As T

Parametri di tipo

T

Tipo a cui verrà eseguito il cast dell'oggetto.

Parametri

o
Object

Oggetto di cui eseguire il cast.

Restituisce

T

Oggetto originale, eseguire il cast al tipo specificato.

Commenti

Questa API viene usata per eseguire il cast di un oggetto al tipo specificato, eliminando i normali controlli di sicurezza dei tipi del runtime. È responsabilità del chiamante garantire che il cast sia legale. No InvalidCastException verrà generata.

Il comportamento di è definito solo se l'operazione (T)o di Unsafe.As<T>(o) cast "sicura" tipica avrebbe avuto esito positivo. L'uso di questa API per aggirare i cast che altrimenti non sono riusciti non è supportato e potrebbe causare instabilità del runtime.

Per applicare l'utilizzo corretto, gli sviluppatori potrebbero considerare l'uso di un cast standard o un'asserzione di sola debug nel codice, come illustrato negli esempi seguenti.

void ReinterpretCastAndUse_Sample1(object o)
{
  // Assume that we know through some other means that 'o' is a string,
  // and we want our library's debug builds to verify this.
  // One way to do this is through a standard-style cast.
  // A standard-style cast will throw InvalidCastException at runtime if the cast fails.
  // n.b. Casts of null objects to reference types will succeed.

#if DEBUG
  string s = (string)o;
#else
  string s = Unsafe.As<string>(o);
#endif

  DoSomethingWith(s);
}

void ReinterpretCastAndUse_Sample2(object o)
{
  // Another way to check this is through a debug-only assert.
  // Failed assertions will trigger attached debuggers or terminate the application immediately.
  // Calls to Debug.Assert are removed from release builds.

  Debug.Assert(o is null or string, "Unsafe.As call below is illegal!");
  string s = Unsafe.As<string>(o);

  DoSomethingWith(s);
}

Si applica a

As<TFrom,TTo>(TFrom)

Origine:
Unsafe.cs
Origine:
Unsafe.cs
Origine:
Unsafe.cs

Reinterpreta il puntatore gestito specificato come nuovo puntatore gestito a un valore di tipo TTo.

public:
generic <typename TFrom, typename TTo>
 static TTo % As(TFrom % source);
public static ref TTo As<TFrom,TTo> (ref TFrom source);
static member As : 'From -> 'o
Public Shared Function As(Of TFrom, TTo) (ByRef source As TFrom) As TTo

Parametri di tipo

TFrom

Tipo di puntatore gestito da reinterpretare.

TTo

Tipo desiderato del puntatore gestito.

Parametri

source
TFrom

Puntatore gestito da reinterpretare.

Restituisce

TTo

Puntatore gestito a un valore di tipo TTo.

Commenti

Questa API è concettualmente simile a quella di reinterpret_cast<>C++. È responsabilità del chiamante garantire che il cast sia legale. Nessun controllo di runtime verrà eseguito.

Viene reinterpretato solo il puntatore gestito. Il valore a cui si fa riferimento rimarrà invariato. Si consideri l'esempio seguente.

int[] intArray = new int[] { 0x1234_5678 }; // a 1-element array
ref int refToInt32 = ref intArray[0]; // managed pointer to first Int32 in array
ref short refToInt16 = ref Unsafe.As<int, short>(ref refToInt32); // reinterpret as managed pointer to Int16
Console.WriteLine($"0x{refToInt16:x4}");

L'output di questo programma dipende dalla endianità del computer corrente. Nelle architetture big-endian questo codice restituisce 0x1234. Nelle architetture little-endian questo codice restituisce 0x5678.

Quando si esegue il cast di un puntatore gestito da un tipo più stretto a un tipo più ampio, il chiamante deve assicurarsi che la dereferenza del puntatore non incorra in un accesso out-of-bounds. Il chiamante è inoltre responsabile della garanzia che il puntatore risultante sia allineato correttamente per il tipo a cui si fa riferimento. Per altre informazioni sui presupposti di allineamento, vedere ECMA-335, Sec. I.12.6.2 ("Allineamento").

Si applica a