将标准 C++ int 转换为 String^

Convert standard C++ int to String^

我想在 Visual C++/CLI 环境中将 std::int 转换为 System::String^。我知道我们可以使用 Microsoft 提供的包含模板函数的库 marshal_as

#include <msclr\marshal.h>

using namespace msclr::interop;

int count = 1;
String^ Apple;
Apple = marshal_as<String^>(count);

我无法运行这个语句,因为我遇到了这个错误

'msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>::marshal_as':
 This conversion is not supported by the library or the header file needed for 
this conversion is not included.

marshal_as 用于在本机类型和它们的托管等效类型之间进行转换。它不是通用的 "convert anything to anything" 工具。要将整数转换为 .NET String,您可以使用 std::to_string 转换为 C++ 字符串,然后使用 marshal_as 转换为 String,或者使用 Int.ToString ] 如果你想做转换 .NET-side.

int count = 100; 
System::String^ s = count.ToString();