使用CLR和C#处理委托事件 [英] Handle delegate event with CLR and C#

查看:378
本文介绍了使用CLR和C#处理委托事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CLR和C#创建基于事件的调用。但我对此并不了解。我将在下面解释我的代码



我的解决方案有一个3个单独的项目



Project1(C ++ with CLR支持构建) - > Project2(带有CLR支持构建的C ++) - > Project3(C#)



Project1(支持CLR Build的C ++)



I'm trying to create event based call from CLR and C#. But I don't have a idea about it. I'll explain my code below

My solution has a 3 separate project

Project1 (C++ with CLR support build) -> Project2(C++ with CLR support build)-> Project3 (C#)

Project1 (C++ with CLR Build support)

class MB
{     
virtual void ShowMessage()
{
}
};

class MA
{
MB obj;
public: 

void PrintMessage()
{
obj.ShowMessage()
}
};





Project2(C ++使用CLR Build支持)





Project2 (C++ with CLR Build support)

class WA : public MB
{
virtual void ShowMessage() override 
{

}
};

class WB
{
WA wObj;
void GetData()
{
    wObj= new MB();
    wObj.PrintMessage();


}
};



基本上如果我调用wObj.PrintMessage()函数那么它将调用WA类ShowMessage()函数。直到这一点它才能正常工作。



Project3(C#)




Basically if i call wObj.PrintMessage() function then it will call WA class ShowMessage() function. Till this point it's working fine.

Project3 (C#)

Class MC
{
void PrintMessage()
{
}
};



每当调用MA.PrintMessage(Project1)函数时,我都会尝试调用MC.PrintMessage()函数。


I'm trying to invoke MC.PrintMessage() function whenever the MA.PrintMessage (Project1) function called.

推荐答案

您需要在C ++代码中创建一个事件,然后在.NET代码中订阅它:[ ^ ];这是一个具体的例子:[ ^ ]。



您的C ++事件应该在托管的'ref Class中;请参阅:[ ^ ]。
You need to create an Event in your C++ code, and then subscribe to it in your .NET code: [^]; here's a specific example: [^].

Your C++ Event should be in a managed 'ref Class; see: [^].


这篇关于使用CLR和C#处理委托事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆