C++/CLI - C# 事件的托管类
我有一个 C++ 类,它触发一些方法,例如事件。
class Blah {
virtual void Event(EventArgs e);
}
我如何包装它,以便每当调用该方法时都会调用 C#(托管)事件?
我想到继承该类并重载事件方法,然后以某种方式调用托管事件。 我只是不确定如何实际做到这一点。
I have a c++ class that triggers some method like an event.
class Blah {
virtual void Event(EventArgs e);
}
How can I wrap it so whenever the method is called a C# (managed) event will be called?
I thought of inheriting that class and overloading the event method, and then somehow call the managed event.
I'm just not sure how to actually do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西(现已编译测试):
Something like this (now compile-tested):
您需要做一些工作来反映 Event() 方法调用,以便它可以被托管类挂钩。让我们实现一个具体的 blah 类来执行此操作:
您现在可以构造一个 blahImpl 并向其传递一个在调用 Event() 方法时调用的函数指针。您可以使用 Marshal::GetFunctionPointerForDelegate() 来获取这样的函数指针,它为委托创建一个存根,该存根可以从非托管代码转换为托管代码,并且还可以存储实例。结合样板代码来包装非托管类:
C# 代码现在可以为 Event 事件编写事件处理程序。我保留了拼写错误,您需要做一些工作将 EvenAtrgs 转换为从 EventArgs 派生的托管类。相应地修改托管事件。
You need to do some work to reflect the Event() method call so it can be hooked by a managed class. Lets implement a concrete blah class that does so:
You can now construct a blahImpl and pass it a function pointer that is called when the Event() method is called. You can use Marshal::GetFunctionPointerForDelegate() to get such a function pointer, it creates a stub for a delegate that makes the transition from unmanaged code to managed code and can store a instance as well. Combined with the boilerplate code to wrap an unmanaged class:
The C# code can now write an event handler for the Event event. I left the spelling error in tact, you need to do some work to convert EvenAtrgs to a managed class that derives from EventArgs. Modify the managed Event accordingly.
创建一个继承自 blah 的类,并让它在构造函数中引用您的托管包装器。重写 Event() 方法,当它被调用时,您可以将该方法转发到您正在存储的托管包装类实例。
请注意,您无法从包含类的外部引发事件,因此您必须将其设为普通委托或调用托管类上的辅助方法来为您引发事件。
Create a class that inherits from blah and have it take a reference to your managed wrapper in the constructor. Override the Event() method and when it gets called you can just forward that method on to the managed wrapper class instance you are storing.
Note that you can't raise an event from outside of the containing class, so you'll have to either make it a plain delegate or call a helper method on the managed class to raise it for you.