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

发布于 2024-07-11 15:11:46 字数 1143 浏览 7 评论 0原文

知道如何初始化指向“混合”类实例中的方法的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-07-18 15:11:46

我刚刚找到了这个问题的答案(当然是由 Nishant Sivakumar 提供的,男人似乎对我所有的 C++/CLI 互操作相关问题都有答案):

http://www.codeproject.com/KB/mcpp/CppCliSupportLib.aspx?display=Print

答案位于“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);
}
};

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:

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);
}
};
因为看清所以看轻 2024-07-18 15:11:46

只有 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文