如何在托管 C++ 中将委托传递给需要托管函数指针的委托
我有以下调用:
ExecutionContext::Run(item->Context, gcnew ContextCallback(item, &WaitQueueItem::Callback), item->State);
其中 WaitQueueItem
如下:
ref class WaitQueueItem
{
public:
WaitQueueItem(){}
WaitCallback^ Callback;
Object^ State;
ExecutionContext^ Context;
};
WaitCallback
在 中定义为
命名空间。public delegate void WaitCallback(Object^ state)
>System::Threading
编译出现以下错误:
错误 C2843: 'Foo::Bar::Data::ContainingClass::WaitQueueItem::Callback':不能 获取托管的非静态数据成员或方法的地址 类型
如果我提供一个与 WaitCallback
委托具有相同签名的函数,它将进行编译。深入研究 dotPeek,我发现用 C# 编写的相同代码
ExecutionContext.Run(item.Context, new ContextCallback(item.Callback), item.State);
如下所示:
ExecutionContext.Run(item.Context, new ContextCallback(item.Callback.Invoke), item.State);
是否有一种方法无需编写包装函数即可传递委托?
提前致谢
I have the following call:
ExecutionContext::Run(item->Context, gcnew ContextCallback(item, &WaitQueueItem::Callback), item->State);
where WaitQueueItem
is the following:
ref class WaitQueueItem
{
public:
WaitQueueItem(){}
WaitCallback^ Callback;
Object^ State;
ExecutionContext^ Context;
};
WaitCallback
is defined as public delegate void WaitCallback(Object^ state)
in the System::Threading
namespace.
Compilation renders the following error:
error C2843:
'Foo::Bar::Data::ContainingClass::WaitQueueItem::Callback' : cannot
take the address of a non-static data member or method of a managed
type
If I provide a function with the same signature as the WaitCallback
delegate it will compile. Digging in with dotPeek, I see that the same code when written in C#
ExecutionContext.Run(item.Context, new ContextCallback(item.Callback), item.State);
looks like this:
ExecutionContext.Run(item.Context, new ContextCallback(item.Callback.Invoke), item.State);
Is there a way to pass the delegate without having to write a wrapper function?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您想要
(这相当于 C# 编译器所做的,绑定对象实例
item->Callback
及其Invoke
方法)Perhaps you want
(which is equivalent to what the C# compiler did, binding the object instance
item->Callback
and itsInvoke
method)