【发布时间】:2014-12-31 18:22:48
【问题描述】:
当我尝试将对象强制转换为我很确定它实现的接口时,我遇到了运行时异常。
我有以下接口:
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? -
在 WinRT 中没有继承类这样的事情。它是在内部使用组合完成的。我认为在这种情况下你可以使用 safe_cast,它应该“跨越”工作,而不仅仅是上下。我猜它是一个寻路演员。