Windows Phone 8.1:带有IList变量的c#回调无法强制转换为vector

本文关键字:回调 vector 转换 Phone 带有 变量 IList Windows | 更新日期: 2023-09-27 18:09:36

我有一个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++/CX包装器,它实现如下:

ref class  CallbackImpl sealed : ICallBack
{
private:
    CallbackImpl(){}
public:
    virtual void GotFileList(FileType type, Windows::Foundation::Collections::IVector<Object^>^ files, ErrorCode code);
}

我的问题是,在发布构建,当c#调用ICallback::GotFileList

_callback.GotFileList(fileType, result as IList<FileInfo>, ErrorCode.EC_NO_ERROR);

抛出异常System.InvalidCastException: Specified cast is not valid。该异常指的是将illist转换为vector。

调试是好的;这意味着,我可以向内部illist中添加值,并调用ICallback::GotFileList, illist的值在c++ vector中没有任何问题。

我比较了发布/调试项目属性(在所有项目中:c#核心库,c++/CX包装器和c++/CX应用程序),并没有发现任何差异,可以解释发布构建上的异常。

任何想法?

Windows Phone 8.1:带有IList变量的c#回调无法强制转换为vector

我找到了简单的答案。由于ILIst是专有数据类型FileInfo,因此无法强制转换为vector。只要我把接口函数签名从IList<FileInfo>改为IList<Object>(和c++包装器中的向量),它就可以工作了。
下面是新的代码片段:

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);
}
c++包装:

ref class  CallbackImpl sealed : ICallBack
{
private:
    CallbackImpl(){}
public:
    virtual void GotFileList(FileType type, Windows::Foundation::Collections::IVector<Object^>^ files, ErrorCode code);
}