如何在c ++中激活c#COM事件? [英] How to fire c# COM events in c++?

查看:124
本文介绍了如何在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#中启动此事件,并通过firebreath将其侦听到javascript。

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

提前感谢。

推荐答案

COM事件与C#事件非常不同。它们更通用,事件监听器必须实现一个接口,并通过IConnectionPoint告诉事件源它。事件源然后通过调用interface方法提出事件。因此,您需要:

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:


  • 编写一个声明方法而不是事件的接口。因此,在您的情况下只需 void Add(ISample sample)

  • 不要在类声明中继承接口,

  • 在您的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文章< a>显示了一个示例。

This MSDN article shows an example.

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

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