C# 本机类是否可以使用.NET事件?

C# 本机类是否可以使用.NET事件?,c#,.net,c++-cli,C#,.net,C++ Cli,知道如何初始化从“混合”类实例指向方法的.NET委托吗 我有“混合”C++类: class CppMixClass { public: CppMixClass(void){ dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState); } ~CppMixClass(void); void UpdateState(S

知道如何初始化从“混合”类实例指向方法的.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(ref)类

当UpdateHealthState是静态方法时,我可以使用它,但我需要指向实例方法的指针

我试过smth,比如:

dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(this, &UpdateHealthState);
但这显然不起作用,因为这不是指向.NET(ref)类(System::Object)的指针(句柄)

ServiceStateEventHandler在C中定义为:


只有.NET类型才能使用事件。我建议创建一个新的托管类来处理事件,并在CppMixClass中组合该类,并在构造过程中向其传递指向CppMixClass的指针。然后,托管事件处理类可以在处理事件时调用CppMixClass上的函数。

我刚刚找到了答案(当然是Nishant Sivakumar,man似乎对我所有与C++/CLI互操作相关的问题都有答案):

答案位于“msclr/event.h”标题中,其中定义了本机类中委托的宏

Nish的代码如下:

class Demo5
{
msclr::auto_gcroot<FileSystemWatcher^> m_fsw;
public:
// Step (1)
// Declare the delegate map where you map
// native method to specific event handlers

BEGIN_DELEGATE_MAP(Demo5)
    EVENT_DELEGATE_ENTRY(OnRenamed, Object^, RenamedEventArgs^)
END_DELEGATE_MAP()

Demo5()
{
    m_fsw = gcnew  FileSystemWatcher("d:\\tmp");
    // Step (2)
    // Setup event handlers using MAKE_DELEGATE
    m_fsw->Renamed += MAKE_DELEGATE(RenamedEventHandler, OnRenamed);
    m_fsw->EnableRaisingEvents = true;
}
// Step (3)
// Implement the event handler method

void OnRenamed(Object^, RenamedEventArgs^ e)
{
    Console::WriteLine("{0} -> {1}",e->OldName, e->Name);
}
};
classdemo5
{
msclr::auto_gcroot m_fsw;
公众:
//步骤(1)
//在映射的位置声明代理映射
//特定事件处理程序的本机方法
开始\u代表\u映射(演示5)
事件\u委托\u条目(OnRenamed,Object^,RenamedEventArgs^)
结束\u委托\u映射()
演示5()
{
m_fsw=gcnewfilesystemwatcher(“d:\\tmp”);
//步骤(2)
//使用MAKE_委托设置事件处理程序
m_fsw->重命名+=使_委派(重命名为EventHandler,OnRename);
m_fsw->EnableRaisingEvents=true;
}
//步骤(3)
//实现事件处理程序方法
void OnRenamed(对象^,RenamedEventArgs^e)
{
控制台::WriteLine(“{0}->{1}”,e->OldName,e->Name);
}
};
class Demo5
{
msclr::auto_gcroot<FileSystemWatcher^> m_fsw;
public:
// Step (1)
// Declare the delegate map where you map
// native method to specific event handlers

BEGIN_DELEGATE_MAP(Demo5)
    EVENT_DELEGATE_ENTRY(OnRenamed, Object^, RenamedEventArgs^)
END_DELEGATE_MAP()

Demo5()
{
    m_fsw = gcnew  FileSystemWatcher("d:\\tmp");
    // Step (2)
    // Setup event handlers using MAKE_DELEGATE
    m_fsw->Renamed += MAKE_DELEGATE(RenamedEventHandler, OnRenamed);
    m_fsw->EnableRaisingEvents = true;
}
// Step (3)
// Implement the event handler method

void OnRenamed(Object^, RenamedEventArgs^ e)
{
    Console::WriteLine("{0} -> {1}",e->OldName, e->Name);
}
};