我有一个C#
windows phone 8.1 Visual Studio(2013)项目,它声明了一个回调接口
public interface ICallBack { /// <summary> /// The Child Callback must override this method and this will be fired when time comes /// </summary> /// <param name="files">The resultant files </param> /// <param name="code">Error code</param> void GotFileList(FileType type,IList<FileInfo> files,ErrorCode code); }
我有一个C/C++X包装器,它实现如下:
ref class CallbackImpl sealed : ICallBack { private: CallbackImpl(){} public: virtual void GotFileList(FileType type,Windows::Foundation::Collections::IVector<Object^>^ files,ErrorCode code); }
我的问题是在Release版本中,当C#调用ICallback :: GotFileList时
_callback.GotFileList(fileType,result as IList<FileInfo>,ErrorCode.EC_NO_ERROR);
它抛出异常System.InvalidCastException:指定的强制转换无效.该异常是指将IList转换为IVector.
调试很好;意思是,我可以向内部IList添加值并调用ICallback :: GotFileList,并且IList值在C IVector中没有任何问题.
我比较了发布/调试项目属性(在所有项目中:C#核心库,C/C++X包装器和C/C++X应用程序),并没有发现任何可以解释发布版本上的异常的差异.
有任何想法吗?
解决方法
我找到了简单的答案.由于ILIst属于专有数据类型FileInfo,因此无法转换为IVector.一旦我将接口函数签名更改为IList< Object>而不是IList< FileInfo> (以及分别在c包装中的IVector),一切正常.
这是新的代码段:
这是新的代码段:
public interface ICallBack { /// <summary> /// The Child Callback must override this method and this will be fired when time comes /// </summary> /// <param name="files">The resultant files </param> /// <param name="code">Error code</param> void GotFileList(FileType type,IList<Object> files,ErrorCode code); }
包装纸:
ref class CallbackImpl sealed : ICallBack { private: CallbackImpl(){} public: virtual void GotFileList(FileType type,ErrorCode code); }