1

在 C# 中可以编写

typeof(IInterface<>).

C++/CLI 中有类似的东西吗?

ISYMessageDispatcher<>::typeid

无法编译,因为编译器崩溃。

在此处输入图像描述

4

2 回答 2

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'

有关解决方法,请参阅我对相关问题的回答“如何在 C++/CLI 中检查泛型类型?”

于 2012-08-06T15:21:27.797 回答
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]

于 2014-01-20T12:24:55.327 回答