次の方法で共有


方法 : C++ で is キーワードと as C# キーワードを実装する

更新 : 2007 年 11 月

このトピックでは、Visual C++ で is キーワードと as C# キーワードの機能を実装する方法について説明します。

詳細については、「is (C# リファレンス)」および「as (C# リファレンス)」を参照してください。

使用例

// CS_is_as.cpp
// compile with: /clr
using namespace System;

interface class I {
public:
   void F();
};

ref struct C : public I {
   virtual void F( void ) { }
};

template < class T, class U > 
Boolean isinst(U u) {
   return dynamic_cast< T >(u) != nullptr;
}

int main() {
   C ^ c = gcnew C();
   I ^ i = safe_cast< I ^ >(c);   // is (maps to castclass in IL)
   I ^ ii = dynamic_cast< I ^ >(c);   // as (maps to isinst in IL)

   // simulate 'as':
   Object ^ o = "f";
   if ( isinst< String ^ >(o) )
      Console::WriteLine("o is a string");
}

o is a string

参照

その他の技術情報

C++ の他の .NET 言語との相互運用性