【发布时间】:2017-05-07 05:35:39
【问题描述】:
我有一个 COM 组件,我们使用 TLBIMP 工具创建了一个互操作 dll,当访问默认接口时,它有时会失败并出现 InvalidCastException(无法转换类型为 System.__ComObject' 的 COM 对象)
private void SomeMethod(IMyInterface iObj)
{
**//Following cause error InvalidCastException, not always but Intermittently**
//MyComponent comp = new MyComponentClass();
//comp= iObj as MyComponent
**//The below code works fine.**
IMyInterface comp = new MyComponentClass();
comp = iObj;
}
IDL 声明
coclass MyComponent
{
[default] interface IMyInterface;
};
知道为什么上述函数中的注释代码会因错误而失败吗? MyComponent comp = new MyComponentClass() 行也应该返回默认接口,在这种情况下,LHS 变量不应该是接口类型。
【问题讨论】:
标签: com com-interop atl