次の方法で共有


__typeof

 

発行: 2016年4月

メモ   このトピックは、C++ マネージ拡張のバージョン 1 にのみ対応しています。 この構文は、バージョン 1 のコードを保守するためだけに使用してください。 参照してください typeid (C++ コンポーネント拡張) については、新しい構文で同等の機能を使用します。

指定された型の System::Type を返します。

__typeof(
typename
)

それぞれの文字について以下に説明します。

  • typename
    System::Type の名前を必要とするマネージ型の名前。 マネージ プログラムでは、ネイティブ型は一部、共通言語ランタイムの型にエイリアスされることに注意してください。 たとえば、intSystem::Int32 のエイリアスです。

解説

__typeof 演算子を使用すると、指定した型の System::Type 型を取得できます。 また、__typeof を使用して、カスタム属性ブロックの System::Type の値を返すこともできます。 独自属性の作成方法の詳細については、「attribute」を参照してください。

使用例

次の例では、カスタム属性 (AtClass) は、__gc クラス (B) に適用されます。 カスタム属性の値は __typeof で取得されます。

// keyword__typeof.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
using namespace System;

public __gc class MyClass {};

[attribute(All)]
__gc class AtClass {
public:
   AtClass(Type*) {
      Console::WriteLine("in Type * constructor");
   }

   AtClass(String*) {}
   AtClass(int) {}
};

[AtClass(__typeof(MyClass))]   // Apply AtClass attribute to class B
__gc class B {};

int main() {
   Type * mytype = __typeof(B);
   Object * myobject __gc[] = mytype -> GetCustomAttributes(true);
   Console::WriteLine(myobject[0]);
}

出力

in Type * constructor
AtClass