接口继承和铸造 [英] Interface Inheritance and Casting

查看:167
本文介绍了接口继承和铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了我从另一个(COM)接口继承的接口:

I have created an interface which I am inheriting from another (COM) interface:

public interface IDTSComponentMetaData : IDTSComponentMetaData90 { }

这就是它。

这背后的原因是,这取决于哪一个版本的SQL Server我正在与工作的,所述碱可以是IDTSComponentMetaData90(2005)或IDTSComponentMetaData100(2008)。而不是条件编译代码每个参考IDTSComponentMetaData90 / IDTSComponentMetaData100,我想使用的版本无关的接口,这将只是换合适的实际接口。

The reason behind this is, depending on which version of SQL Server I am working with, the base may be IDTSComponentMetaData90 (2005) or IDTSComponentMetaData100 (2008). Rather than conditionally compile every reference to IDTSComponentMetaData90 / IDTSComponentMetaData100 in code, I'd like to use the version-neutral interface which will simply wrap the proper real interface.

问题是SSIS通过我在一个关键的点对象到原生界面,我需要将其转换为我的封装接口:

The problem is that SSIS passes me an object to the native interface at one key point, and I need to cast it to my wrapper interface:

#if SQL2005
public void Initialize(IDTSComponentMetaData90 c,IServiceProvider s) {
#elif SQL2008
public void Initialize(IDTSComopnentMetaData100 c,IServiceProvider s) {
#endif
  m_ComponentMetaData = (IDTSComponentMetaData) c;
  m_ServiceProvider = s;
}

这编译没有问题,但在运行时,我得到一个'无法投类型的COM对象的系统.__ ComObject'的接口类型MyNameSpace.IDTSComponentMetaData。此操作失败的原因是对IID失败,因为下面的错误接口{483E01E7-001C-35E4-Ac9f-4B0C1B81E409}'的COM组件调用QueryInterface:不支持此接口(异常来自HRESULT:0x80004002(E_NOINTERFACE))

This compiles with no problem, but at run time, I get an 'Unable to cast COM object of type 'System.__ComObject' to interface type 'MyNameSpace.IDTSComponentMetaData'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{483E01E7-001C-35E4-Ac9f-4B0C1B81E409}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

是我在做什么完全错了?

Is what I am doing totally wrong?

推荐答案

任何原因你根据SQL Server版本不只是别名接口?

Any reason you don't simply alias the interface depending on SQL Server version?

#if SQL2005
using IDTSComponentMetaData = IDTSComponentMetaData90;
#elif SQL2008
using IDTSComponentMetaData = IDTSComopnentMetaData100;
#endif

这篇关于接口继承和铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆