问题
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