Unsafe.As メソッド

定義

オーバーロード

名前 説明
As<T>(Object)

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

As<TFrom,TTo>(TFrom)

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

As<T>(Object)

ソース:
Unsafe.cs
ソース:
Unsafe.cs
ソース:
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
ソース:
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) where TFrom : allows ref struct where TTo : allows ref struct;
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") を参照してください。

適用対象