2012-08-06 167 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]