使用反射在C#中处理COM事件
我有一个带有主接口和事件接口的COM对象(来自IDL文件): 从上面调用ConnectionPointCookie时-发生错误(大约从本地化消息翻译过来):“无法为事件接口“\uu ComObject”执行advice() 我的代码正确吗?如何将AxBase正确连接到COM对象事件? (对于动态类型,不能在C#代码中使用IAxBitViewerWidgetEvents等) 注意:调用类方法(不是事件)时不会出现问题,例如:使用反射在C#中处理COM事件,c#,qt,reflection,com,activex,C#,Qt,Reflection,Com,Activex,我有一个带有主接口和事件接口的COM对象(来自IDL文件): 从上面调用ConnectionPointCookie时-发生错误(大约从本地化消息翻译过来):“无法为事件接口“\uu ComObject”执行advice() 我的代码正确吗?如何将AxBase正确连接到COM对象事件? (对于动态类型,不能在C#代码中使用IAxBitViewerWidgetEvents等) 注意:调用类方法(不是事件)时不会出现问题,例如: ocx.GetType().InvokeMember("initiali
ocx.GetType().InvokeMember("initialize", System.Reflection.BindingFlags.InvokeMethod,
null, ocx, new object[] { this.argum1, this.argum2 });
p.S.2:以下代码返回一个空数组:
System.Reflection.EventInfo[] arr = ocx.GetType().GetEvents();
我终于让它工作了。主类是这样继承的
public class BitViewerEx : AxHost, IBitViewerEvents
其中IBitViewerEvents
为(基于相应自动生成的.idl文件的内容)
System.Reflection.EventInfo[] arr = ocx.GetType().GetEvents();
public class BitViewerEx : AxHost, IBitViewerEvents
// Methods should be like signals in the Qt active-x class with the bit viewer.
// DispIds are taken from the .idl file for the corresponding methods.
[ComImport, Guid(<eventsId>)]
public interface IBitViewerEvents
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(8)]
void started();
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(9)]
void stopped();
}
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
...
private void Initialize() // BitViewerEx class member
{
try
{
object ocx = GetOcx();
if (ocx != null)
{
ocx.GetType().InvokeMember("initialize", System.Reflection.BindingFlags.InvokeMethod,
null, ocx, new object[] { this.deviceAddress, this.cellAddress });
var cookie = new ConnectionPointCookie(ocx, this, typeof(IBitViewerEvents));
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.ToString());
}
}