C# 可以从托管C++调用.NET可变长度PARAMS方法吗?
我在接口上声明了一个方法,如下所示:C# 可以从托管C++调用.NET可变长度PARAMS方法吗?,c#,params,managed-c++,C#,Params,Managed C++,我在接口上声明了一个方法,如下所示: public interface IInterface { void DoSomething(string format, params object[] arguments); } 我正在动态加载一个使用这个接口实现的托管C++类。接口和实现都是C IInterface^ foo = gcnew Implentation(); 这个电话很好: foo->DoSomething("string", someParam); 此呼叫失败: f
public interface IInterface
{
void DoSomething(string format, params object[] arguments);
}
<>我正在动态加载一个使用这个接口实现的托管C++类。接口和实现都是C
IInterface^ foo = gcnew Implentation();
这个电话很好:
foo->DoSomething("string", someParam);
此呼叫失败:
foo->DoSomething("string");
看起来,如果没有参数可以传递,它就无法解析应该接受任何数量的参数(当然包括零)的方法。我可能会使用nullptr作为占位符,但这很难看,或者我可以添加多余的DoSomethingstring格式重载,这也不太好,但总比让所有调用变得比它们应该的更尴尬要好
我是否遗漏了什么,或者这不受支持?Internet上所有的帮助者都显示了如何在C++中声明PARAMS等价物,但在我的场景中没有帮助。 < P>我不能复制您的问题。 给定C接口和实现:
using System;
namespace SomeNamespace
{
public interface IInterface
{
void Print(string format, params object[] args);
}
public class Implementation : IInterface
{
public void Print(string format, params object[] args)
{
Console.WriteLine(format, args);
}
}
}
此C++/CLI代码工作正常:
using namespace System;
using namespace SomeNamespace;
public ref class Test
{
public:
static void Run()
{
IInterface^ foo = gcnew Implementation();
foo->Print("hello, {0}", "world");
foo->Print("hello, world");
}
};
这里是否缺少另一个元素?还是我没有注意到:只是要清楚,C++类是实现接口还是使用实现它的类?文本说它正在使用另一个类,但是IInterface foo=新的实现;是C代码。我已经更新了问题以便更清楚。C++类拥有C类的实例,C类实现C接口。是的,当然应该是beNo,我相信你已经做到了:这正是我所期望的行为,但是当我尝试我的建议nullptr或重载时,一切都很好。我没有提到的是C++类是通过反射动态加载的,但是在其他情况下这是没有问题的。自从我接触到C++以来,已经有很长一段时间了,所以在管理的世界里,我更可能错过了一些编译器选项或者一些东西。