Unsafe.As メソッド

定義

オーバーロード

As<T>(Object)

指定したオブジェクトを指定した型にキャストします。

As<TFrom,TTo>(TFrom)

指定したマネージド ポインターを、 型の値への新しいマネージド ポインターとして再解釈します TTo

As<T>(Object)

ソース:
Unsafe.cs
ソース:
Unsafe.cs
ソース:
Unsafe.cs

指定したオブジェクトを指定した型にキャストします。

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

型パラメーター

T

オブジェクトがキャストされる型。

パラメーター

o
Object

キャストするオブジェクト。

戻り値

T

指定した型にキャストされた元のオブジェクト。

注釈

この API は、指定された型にオブジェクトをキャストするために使用され、ランタイムの通常の型の安全性チェックが抑制されます。 キャストが有効であることを確認するのは呼び出し元の責任です。 いいえ InvalidCastException がスローされます。

Unsafe.As<T>(o) 動作は、一般的な "安全な" キャスト操作 (T)o が成功した場合にのみ適切に定義されます。 この API を使用して、失敗したキャストを回避することはサポートされておらず、ランタイムが不安定になる可能性があります。

正しい使用を強制するために、開発者は、次の例に示すように、コードで標準キャストまたはデバッグ専用アサートを使用することを検討できます。

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);
}

適用対象

As<TFrom,TTo>(TFrom)

ソース:
Unsafe.cs
ソース:
Unsafe.cs
ソース:
Unsafe.cs

指定したマネージド ポインターを、 型の値への新しいマネージド ポインターとして再解釈します 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

型パラメーター

TFrom

再解釈するマネージド ポインターの型。

TTo

マネージド ポインターの目的の型。

パラメーター

source
TFrom

再解釈するマネージド ポインター。

戻り値

TTo

TToの値へのマネージド ポインター。

注釈

この API は概念的には C++ の reinterpret_cast<>と似ています。 キャストが有効であることを確認するのは呼び出し元の責任です。 ランタイム チェックは実行されません。

マネージド ポインターのみが再解釈されます。 参照される値自体は変更されません。 例を次に示します。

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}");

このプログラムの出力は、現在のマシンのエンディアンによって異なります。 ビッグ エンディアン アーキテクチャでは、このコードは を出力します 0x1234。 リトル エンディアン アーキテクチャでは、このコードは を出力します 0x5678

より狭い型からより広い型にマネージド ポインターをキャストする場合、呼び出し元はポインターを逆参照しても境界外アクセスが発生しないようにする必要があります。 呼び出し元は、参照される型に対して結果のポインターが適切に配置されるようにする責任もあります。 アラインメントの前提条件の詳細については、「 ECMA-335,Sec. I.12.6.2 ("Alignment")」を参照してください。

適用対象