C# では次のように書くことができます
typeof(IInterface<>).
C++/CLI に似たようなものはありますか?
ISYMessageDispatcher<>::typeid
コンパイラがクラッシュするため、コンパイルできません。
(Visual Studio 2010 SP1 を使用して) テストすると、コンパイラはクラッシュしません。これは単なる構文エラーです。
#using <System.dll>
int main(void)
{
System::Console::WriteLine( (System::Collections::Generic::List<>::typeid)->ToString() );
return 0;
}
コンパイルしようとすると、次のようになります。
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01
for Microsoft (R) .NET Framework version 4.00.30319.269
Copyright (C) Microsoft Corporation. All rights reserved.
generictypeid.cpp
generictypeid.cpp(5) : error C2976: 'System::Collections::Generic::List' : too few generic arguments
c:\windows\microsoft.net\framework\v4.0.30319\mscorlib.dll : see declaration of 'System::Collections::Generic::List'
回避策については、関連する質問「C++/CLI でジェネリック型をチェックする方法は?」に対する私の回答を参照してください。
以下は私にとってはうまくいきます:
auto x = gcnew Generic::List<bool>;
if (x->GetType()->GetGenericTypeDefinition() == Generic::List::typeid)
{
System::Console::WriteLine("The generic type is " + Generic::List::typeid); // or x->GetType::GetGenericTypeDefinition()
System::Console::WriteLine("The specific type is " + Generic::List<bool>::typeid); // or x->GetType()
}
これにより出力が得られます
ジェネリック型は System.Collections.Generic.List`1[T] です
特定の型は System.Collections.Generic.List`1[System.Boolean] です