__typeof

 

發佈時間: 2016年4月

注意:本主題只適用於 Managed Extensions for C++ 第 1 版。 這個語法只可用於維護第 1 版的程式碼。 請參閱 typeid (C++ 元件擴充功能) 如需新語法中使用的相同功能。

傳回指定類型的 System::Type

__typeof(
typename
)

其中:

  • typename
    您要命名為 System::Type 之 Managed 類型的名稱。 請注意,在 Managed 程式中,部分原生類型的別名是 Common Language Runtime 中的類型。 例如,intSystem::Int32 的別名。

備註

__typeof 運算子可讓您取得您所指定之類型的 System::Type 類型。__typeof 也可用於在自訂屬性區塊中傳回 System::Type 的值。 如需有關自行建立屬性的詳細資訊,請參閱屬性

範例

下列範例是將自訂屬性 (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