2012-08-06 117 views
1

在C#中,可以写泛型类型定义

typeof(IInterface<>). 

是否有C++/CLI类似的东西?

ISYMessageDispatcher<>::typeid 

由于编译器崩溃而无法编译。

enter image description here

+0

哪个版本编译器?如果从命令行运行cl.exe,它是否会提供任何错误消息? – 2012-08-06 15:16:29

回答

1

当我测试(使用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' 

有关解决方法,请参阅my answer to a related question "How to check a generic type in C++/CLI?"

+0

因此,直接访问类似C#中的泛型类型定义在C++/CLI中根本不可能? – Antineutrino 2012-08-07 05:59:11

+1

@Authutrino:我不知道任何。抱歉。 – 2012-08-07 06:11:19

0

下面的工作对我来说:

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]