本机类是否可以使用 .NET 事件?
知道如何初始化指向“混合”类实例中的方法的 .NET 委托吗?
我有这样的“混合”C++ 类:
class CppMixClass
{
public:
CppMixClass(void){
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
}
~CppMixClass(void);
void UpdateState(System::Object^ sender, DotNetClass::StateEventArgs^ e){
//doSmth
}
}
DotNetClass 在 C# 中实现,并且方法声明可以使用委托。 此行生成错误:
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
error C2276: '&' : illegal operation on bound member function expression
有人对问题有线索吗? 也许是因为 CppMixClass 类不是纯 .NET(参考)类?
当 UpdateHealthState 是静态方法时,我可以使用它,但我需要指向实例方法的指针。
我尝试过类似的操作:
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(this, &UpdateHealthState);
但这显然不起作用,因为这不是指向.NET(ref)类(System::Object)的指针(句柄)。
ServiceStateEventHandler 在 C# 中定义为:
public delegate void ServiceStateEventHandler(object sender, ServiceStateEventArgs e);
感谢您阅读本文:)
Any idea how to initialize .NET delegate that points to method from 'mixed' class instance?
I have 'mixed' C++ class like this:
class CppMixClass
{
public:
CppMixClass(void){
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
}
~CppMixClass(void);
void UpdateState(System::Object^ sender, DotNetClass::StateEventArgs^ e){
//doSmth
}
}
DotNetClass is implemented in C#, and Method declaration is OK with delegate.
This line generates error:
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
error C2276: '&' : illegal operation on bound member function expression
Anyone have a clue about a problem?
Maybe coz CppMixClass class is not a pure .NET (ref) class?
I got this to work when UpdateHealthState is static method, but I need pointer to instance method.
I tried smth like:
dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(this, &UpdateHealthState);
But this obviously doesn't work coz this is not pointer (handle) to .NET (ref) class, (System::Object).
ServiceStateEventHandler is defined in C# as:
public delegate void ServiceStateEventHandler(object sender, ServiceStateEventArgs e);
Thanx for reading this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚找到了这个问题的答案(当然是由 Nishant Sivakumar 提供的,男人似乎对我所有的 C++/CLI 互操作相关问题都有答案):
http://www.codeproject.com/KB/mcpp/CppCliSupportLib.aspx?display=Print
答案位于“msclr/event.h”标题中,其中定义了本机类中的委托宏。
Nish的代码如下:
I just found answer to this(of course by Nishant Sivakumar, man seems to have answers to all my C++/CLI interop related problems):
http://www.codeproject.com/KB/mcpp/CppCliSupportLib.aspx?display=Print
Answer is located in "msclr/event.h" header, where macros for delegates in native classes are defined.
Nish's code is following:
只有 .NET 类型可以使用事件。 我建议创建一个新的托管类来处理该事件,并在 CppMixClass 中组合该类,并在构造期间向其传递一个指向 CppMixClass 的指针。 然后,您的托管事件处理类可以在处理事件时调用 CppMixClass 上的函数。
Only .NET types can use events. I suggest creating a new managed class that handles the event, and compose that class within CppMixClass, and pass it a pointer to CppMixClass during construction. Your managed event handling class can then call a function on CppMixClass when it handles an event.