可以从托管 C 调用 .NET 可变长度参数方法吗?
我在接口上声明了一个方法,如下所示:
public interface IInterface
{
void DoSomething(string format, params object[] arguments);
}
我正在动态加载使用此接口的实现的托管 C++ 类。接口和实现都是C#。
IInterface^ foo = gcnew Implentation();
这个调用很好:
foo->DoSomething("string", someParam);
这个调用失败:
foo->DoSomething("string");
看起来如果没有参数传递,它就无法解析应该接受任意数量参数(当然包括零)的方法。我可能可以使用 nullptr 作为占位符,但这非常难看,或者我可以添加一个多余的 DoSomething(string format)
重载,这也不是很好,但比让所有调用都比应有的更尴尬要好是。
我是否缺少某些内容或者不支持此功能?互联网上的所有帮助程序都展示了如何在 C++ 中声明等效的参数,但这对我的场景没有帮助。
I have a method declared on an interface like so:
public interface IInterface
{
void DoSomething(string format, params object[] arguments);
}
I am dynamically loading a managed c++ class that uses an implementation of this interface. The interface and inmplementation are both C#.
IInterface^ foo = gcnew Implentation();
This call is fine:
foo->DoSomething("string", someParam);
This call fails:
foo->DoSomething("string");
It looks like if there are no parameters to pass it just can't resolve the method which should accept any number of params (including zero of course). I could probably use a nullptr as a placeholder but that's pretty ugly or I could add a superfluous overload of DoSomething(string format)
which isn't great either but better than making all calls more awkward than they should be.
Am I missing something or is this not supported? All the helpers on the internet show how to declare the params equivalent in c++ but that doesn't help in my scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法复制你的问题。
给定 C# 接口 &实现:
此 C++/CLI 代码工作正常:
这里是否还缺少另一个元素?或者我错过了重点:)
I can't replicate your problem.
Given the C# interface & implementation:
This C++/CLI code works fine:
Is there another element that is missing here? Or have I missed the point :)