C# component events?(C# 组件事件?)
问题描述
我正在尝试编写一个公开事件的 C# 组件.该组件将由非托管 C++ 应用程序导入.根据一些教程,我想出了这段代码(用于 C# 端):
I am attempting to write a C# component which will expose events. The component is to be imported by an unmanaged C++ application. According to a few tutorials I have come up with this code (for the C# side):
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++ 代码有一个简单的:
Now my c++ code has a simple:
#import "COMTest.tlb" named_guids raw_interfaces_only
这会生成一个 tlh 文件.此 tlh 文件包含除我的事件 (OnTest) 之外的所有内容.我做错了什么?
Which generates a tlh file. This tlh file contains everything but my event (OnTest). What am I doing incorrectly?
推荐答案
COM 事件接收器对于外行来说是相当邪恶的.
COM Event Sinks are pretty evil to the uninitiated.
步骤基本是
- 创建一个传出(源)接口
- 实施一个IConnectionPointContainer 和IConnectionPoint 接口,使用这些传递一个客户端实现源接口的
好消息是,在互操作命名空间中有一些属性可以帮助您(大部分)自动执行此操作(ComSourceInterfacesAttribute) 有一个很好的使用例子这里.
The good news is that in the interop namespace there are attributes to help you do this (mostly) automatically (ComSourceInterfacesAttribute) There is a decent example of its usage here.
这篇关于C# 组件事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 组件事件?
基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
