RuntimeBinding exception working with vectors of string

大兔子大兔子 提交于 2020-01-06 06:47:06

问题


I have a C# UWP application that uses a custom UWP Runtime Library written in Visual C++.

In one of my Visual C++ classes I convert a vector of strings to a collection of platform strings, which are understandable to the C# UWP application.

My problem is a Windows Runtime Bindable exception is thrown only in release mode of the application. Everything works perfectly in debug mode.

Visual C++:

// internalModel.cpp - the model I get my vector of strings from
std::vector<std::string> InternalModel::getAllPackages() {
    std::vector<std::string> names;
    for (auto i = 0U; i < packageOptions.size(); i++) {
        // these are strings
        names.push_back(packageOptions[i]["name"]);
    }
    return names;
}

// .h - the method signature called by my C# program
Windows::Foundation::Collections::IVector<Platform::String^>^ GetAllPackages();

// .cpp - the method body called by my C# program
Windows::Foundation::Collections::IVector<Platform::String^>^ VisualModel::GetAllPackages() {
    auto s_packages = internalModel->getAllPackages();
    auto p_packages = ref new Platform::Collections::Vector<Platform::String^>();
    for each (auto s_package in s_packages)
    {
        p_packages->Append(WinUtil::toPlat(s_package));
    }
    return p_packages;
}

// WinUtil.h - the util class that converts standard string vector to platform collection of strings
template <class A, class B>
static Platform::Collections::Vector<B^>^ toPlat(std::vector<A*>* s_vector) {
    auto p_vector = ref new Platform::Collections::Vector<B^>();
    for each (A* s_elem in s_vector) {
        p_vector->Append(ref new B(s_elem));
    }
    return p_vector;
}

C#:

// where I call the Visual C++ from
var packages = VisualLib.GetAllPackages();
foreach (var package in packages)
{
    // package should be string here         
}

I'm unable to figure out what is causing the exception because my debugger can't enter the UWP Runtime Library.

Perhaps the way I'm converting the vector to platform collection is not right, but then again, it works in debug mode just fine. Also other parts of my program use this same utility function: static Platform::Collections::Vector<B^>^ toPlat(std::vector<A*>* s_vector)

The exception thrown:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Exception_WasThrown, Microsoft.CSharp.RuntimeBinder.RuntimeBinderException. For more information, visit http://go.microsoft.com/fwlink/?LinkId=623485'

来源:https://stackoverflow.com/questions/48529050/runtimebinding-exception-working-with-vectors-of-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!