我有一个用 VS2010 C# 构建的简单 .NET dll,它公开了一个类的 2 个静态成员
public class Polygon
{
public static void Test(int test) {}
public static void Test(List<int> test) {}
}
然后我从 VS2010 C++ 创建了一个控制台应用程序,并在 _tmain 上方添加了这个函数
extern "C" void TestMe()
{
Polygon::Test(3);
}
添加引用和编译给了我这个错误
1>WierdError.cpp(9): error C2526: 'System::Collections::Generic::List<T>::GetEnumerator' : C linkage function cannot return C++ class 'System::Collections::Generic::List<T>::Enumerator'
1> with
1> [
1> T=int
1> ]
1> WierdError.cpp(9) : see declaration of 'System::Collections::Generic::List<T>::Enumerator'
1> with
1> [
1> T=int
1> ]
1> WierdError.cpp(9) : see reference to class generic instantiation 'System::Collections::Generic::List<T>' being compiled
1> with
1> [
1> T=int
1> ]
我的一些观察:
- 如果我删除 extern "C",它将成功编译
- 如果我重命名
Test(List<int> test)
为,它将成功编译Test2(List<int> test)
我的问题是,出了什么问题以及如何从 C++ 端修复它。
我目前的解决方法是重命名 C# 中的方法,但我宁愿不必这样做,我觉得我的 C++ 项目中可能缺少一个设置。
编辑:
我在 C++ 中找到了更好的解决方法,看起来我可以将 .NET 调用包装在另一个函数中。
void wrapper()
{
Polygon::Test(3);
}
extern "C" void TestMe()
{
wrapper();
}
必须这样做似乎很愚蠢,我想知道这是否是编译器错误?让我害怕的是使用这样的方法并且不得不担心 C# 开发人员可能会在以后添加这样的静态方法并破坏 C++ 构建。