如何在火灾C ++ C#COM事件? [英] How to fire c# COM events in c++?

查看:126
本文介绍了如何在火灾C ++ C#COM事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中的接口中,我已宣布例如事件:

I have an Interface in c# in which i have declared event for example:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("3839C631-AEDE-4535-9A28-15F4BFCA0C5A")]
public Interface Isample 
{
     event OnAdd(Isample sample);

}

[ClassInterface(ClassInterfaceType.None)]
[Guid("E81B32A2-B98C-48C0-A235-17771EE001D6")]
[ComVisible(true)]
public class Sample:Isample
{
    //'''''''''''''''''''''
}

我想火在C#本次活动,并听成JavaScript通过firebreath。

I want to fire this event in c# and listen it into javascript through firebreath.

感谢。

推荐答案

COM事件是从C#事件有很大不同。他们更通用的,事件的监听器的必须实现一个接口,通过告知有关的IConnectionPoint它的事件源。事件源然后简单地通过调用接口方法引发的事件。因此,你需要:

COM events are very different from C# events. They are much more generic, the event listener must implement an interface and tell the event source about it through IConnectionPoint. The event source then "raises" an event simply by calling the interface method. You thus need to:


  • 写声明的方法,而不是事件的接口。所以,简单地无效添加(ISample样品)你的情况。

  • 请不要在继承类声明的接口,它是事件侦听器的工作来实现它。

  • 在C#类的名字完全相同的方法匹配的接口

  • 声明一个公共事件允许CLR实现的IConnectionPointContainer你告诉它有关的接口,使用 [ComSourcesInterfaces] 属性。

  • Write an interface that declares methods, not events. So simply void Add(ISample sample) in your case.
  • Don't inherit the interface in your class declaration, it is the job of the event listener to implement it.
  • Declare a public event in your C# class whose name exactly matches the method in the interface
  • Allow the CLR to implement IConnectionPointContainer for you by telling it about the interfaces, use the [ComSourcesInterfaces] attribute.

MSDN文章显示了一个例子。

这篇关于如何在火灾C ++ C#COM事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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