C#组件事件?

我正在尝试编写一个C#组件,它将公开事件。 该组件将由非托管C ++应用程序导入。 根据一些教程我已经提出了这个代码(对于C#方面):

namespace COMTest { [ComVisible(true), Guid("02271CDF-BDB9-4cfe-B65B-2FA58FF1F64B"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface ITestEvents { void OnTest(); } [ComVisible(true), Guid("87BA4D3A-868E-4233-A324-30035154F8A4")] public interface ITest { void RaiseTest(); } // End of ITest [ComVisible(true), Guid("410CD174-8933-4f8c-A799-8EE82AF4A9F2"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(ITestEvents))] public class TestImplimentation : ITest { public TestImplimentation() { } public void RaiseTest() { if (null != OnTest) OnTest(); } public delegate void Test (); //No need to expose this delegate public event Test OnTest; } } 

现在我的c ++代码很简单:

 #import "COMTest.tlb" named_guids raw_interfaces_only 

这会生成一个tlh文件。 这个tlh文件包含除我的事件(OnTest)之外的所有内容。 我做错了什么?

COM事件接收器对于不熟悉的人来说是非常邪恶的。

步骤基本上是

  • 创建一个传出(源)接口
  • 实现IConnectionPointContainer和IConnectionPoint接口,使用这些接口传递源接口的客户端实现

消息是在interop命名空间中有一些属性可以帮助你(大多数情况下)自动执行此操作( ComSourceInterfacesAttribute )这里有一个很好的例子。