Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用反射在C#中处理COM事件_C#_Qt_Reflection_Com_Activex - Fatal编程技术网

使用反射在C#中处理COM事件

使用反射在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

我有一个带有主接口和事件接口的COM对象(来自IDL文件):

从上面调用ConnectionPointCookie时-发生错误(大约从本地化消息翻译过来):“无法为事件接口“\uu ComObject”执行advice()

我的代码正确吗?如何将AxBase正确连接到COM对象事件? (对于动态类型,不能在C#代码中使用IAxBitViewerWidgetEvents等)

注意:调用类方法(不是事件)时不会出现问题,例如:

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());
    }
}