当我尝试将一个对象强制转换为它实现的接口时,出现了运行时异常。
我有以下接口:
public interface class ISMILTimeContainer;
public interface class ISMILSequence : ISMILTimeContainer;
public interface class ISMILParallel : ISMILTimeContainer;
我有以下类:
ref class TimeContainer : public ISMILTimeContainer;
ref class Sequence : public TimeContainer, ISMILSequence;
ref class Parallel : public TimeContainer, ISMILParallel;
接下来,我尝试以下操作:
ISMILTimeContainer^ container = getSequence(); // returns a Sequence^
ISMILSequence^ sequence = static_cast<ISMILSequence^>(container);
这会在运行时抛出异常:
Platform::InvalidCastException ^ 在内存位置0x04AFD83C。HRESULT: 0x80004002 不支持此接口
据我所知,这应该是有效的。我尝试做什么有问题吗?还是症状表明实现存在问题(某些内容与上述声明不同)?
getSequence
吗?我尝试重现了你的代码,但没有出现异常。对我来说它是有效的。我改变的只是调用getSequence
的那一行代码,改成了这样:ISMILTimeContainer^ container = ref new Sequence();
也许getSequence
没有返回一个Sequence
? - chue x