我有一个COM组件,它将事件暴露给组件的使用者,然后是组件使用的第三方dll,它会触发它处理的事件。处理完第三方事件后,我会向消费者触发COM公开事件。
// delegate
public delegate void RequestGetOpenTableDelegate(GetOpenTableEventArgs e);
// interface
[ComVisible(true)]
[Guid("DAA7E8EE-1DB3-4129-964E-9E9985E4000B")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface RequestEvents
{
[DispId(0x60020000)]
void RequestGetOpenTable(GetOpenTableEventArgs e);
}
// class
[ComVisible(true), GuidAttribute("8CE90A62-D6D8-4b2a-A9C0-21688A0CDEB8")]
[ProgId("Connector")]
[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(RequestEvents))]
public class Connector
{
public Connector()
{
MyExternalDLL dll = new MyExternalDLL();
dll.requestEvent += new MyExternalDLLEventHandler(ProcessRequest);
}
[ComVisible(false)]
private void ProcessRequest(string xml)
{
// do some xml parsing
// firing com exposed event
GetOpenTableEventArgs e = new GetOpenTableEventArgs(obj);
RequestGetOpenTable(e);
}
public event RequestGetOpenTableDelegate RequestGetOpenTable;
}
从com客户端应用程序进行测试时,事件正常,看起来像这样;
Method = {Void RequestOpenTable(Connector.CloseTableEventArgs)}
Target = {System.__ComObject}
但是当第三方dll事件触发时,RequestGetOpenTable委托为null。我的猜测是我必须创建代表......
System.Reflection.MethodInfo method = typeof(RequestEvents).GetMethod("RequestGetOpenTable");
RequestGetOpenTableDelegate dlg = (RequestGetOpenTableDelegate)Delegate.CreateDelegate(typeof(RequestGetOpenTableDelegate), method);
dlg.Invoke(e);
这会导致绑定错误。如何创建委托以便在com客户端应用程序中触发?