我正在编写需要连接到COM事件的C#代码。我实现了使用 的IConnectionPointContainer和的IConnectionPoint的这样:连接到C#中的COM事件 - 支持托管服务器和非托管服务器
IConnectionPointContainer connectionPointContainer = internalGenerator as IConnectionPointContainer;
if (connectionPointContainer == null)
{
Debug.Fail("The script generator doesn't support the required interface - IConnectionPointContainer");
throw new InvalidCastException("The script generator doesn't support the required interface - IConnectionPointContainer");
}
Guid IID_IScriptGeneratorEvents = typeof(IScriptGeneratorCallback).GUID;
connectionPointContainer.FindConnectionPoint(ref IID_IScriptGeneratorEvents, out m_connectionPoint);
m_connectionPoint.Advise(this, out m_cookie);
的问题是,当COM服务器的.NET是实际执行的(比如说,C#),净创建后,它处理它作为一个。 Net对象,而不是COM对象。由于.Net对象没有实现IConnectionPointContainer接口,因此当试图将对象转换为该接口时,我会得到空值。
任何想法我该如何解决这个问题? 我当然可以在C#COM服务器上自己实现IConnectionPointContainer,但是我想要一个更简单的解决方案,我可以很容易地向需要实现COM服务器的其他开发人员解释。
P.S我必须使用IConnectionPointContainer,因为COM服务器可以在非.Net(C++,Java)中实现。
谢谢, 因巴尔