当我尝试将COMAddIn强制转换回runtimeInterface时,我得到一个异常。 我尝试执行以下操作:
foreach (COMAddIn addin in this.wordInstance.COMAddIns)
{
if (addin != null)
{
if (addin.Description == "Word2013AddIn")
{
this.runtimeInterface = (IRuntimeInterface)addin.Object;
}
}
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IRuntimeInterface
{
event EventHandler<EventArgs> Saving;
void OnSaving(EventArgs e);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class RuntimeInterface : StandardOleMarshalObject, IRuntimeInterface
{
public event EventHandler<EventArgs> Saving;
public void OnSaving(EventArgs e)
{
EventHandler<EventArgs> handler = Saving;
if (handler != null)
{
handler(this, e);
}
}
}
这是我得到的例外:
Unable to cast COM object of type 'System.__ComObject' to interface type
'IRuntimeInterface'. This operation failed because the QueryInterface call on the COM
component for the interface with IID '{E3FE02D2-EB95-383D-8301-3AF3260E92ED}' failed
due to the following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).