我在VS2010中的解决方案有一个在c#项目中引用的CLI项目。我在CLI中有一个名为do_something的抽象类。在C#中,我从它继承了DoSomething类。我想通过传递c#实现作为抽象类参数来运行C++的c#实现,但抛出了“不受语言支持”的异常。语言不支持“方法”
CLI/C++
//abstract class
public ref class do_something{
public:
virtual void do_it()=0;
};
//using an implementation of the abstract class
public ref class cpp_caller{
public:
void run(do_something% doer){
cout<<"run c# from c++"<<endl;
doer.do_it();
}
};
C#
//implementation of abstract class
class DoSomething : do_something
{
public override void do_it()
{
Console.WriteLine("call from c#");
}
}
//inside main
DoSomething csharp_implementation = new DoSomething();
cpp_caller caller = new cpp_caller();
caller.run(csharp_implementation);
的C++项目编译,但编译的C#代码的最后一行时,编译器会引发异常: '跑' 不被支持的语言
注意:先前的堆栈溢出解决方案没有帮助!调用run.do_it()在c#中工作正常。最后,CLI编译器不喜欢使用'^'或'&'通过引用将参数传递给run方法。
它应该使用'^'。它也应该使用'^%'并在C#中使用'ref'关键字。请显示你失败的尝试。 –
当使用'^','^%'或'%^'时,cli编译器会抛出:'.do_it'的左边必须有class/struct/union。 –
'^'是'*'管理的等价物。你需要使用' - >',而不是'.'。 –