marshal_as
此方法将在本机和托管环境之间转换数据。
语法
To_Type marshal_as<To_Type>(
From_Type input
);
参数
input
[in] 要封送给 To_Type
变量的值。
返回值
To_Type
类型的变量,是 input
的转换值。
备注
此方法是一种简化的方法,用于在本机类型和托管类型之间转换数据。 若要确定支持哪些数据类型,请参阅 C++ 中的封送概述。 某些数据转换需要上下文。 可以使用 marshal_context 类转换这些数据类型。
如果尝试封送一对不支持的数据类型,marshal_as
则会在编译时生成错误 C4996。 有关详细信息,请阅读此错误提供的消息。 可以针对多个已弃用的函数生成 C4996
错误。 其中一个示例是尝试封送一对不支持的数据类型。
封送库由多个头文件组成。 任何转换只需要一个文件,但如果需要进行其他转换,则可以包含其他文件。 若要查看哪些转换与哪些文件相关联,请查看 Marshaling Overview
中的表。 无论要执行什么转换,命名空间要求始终有效。
在输入的参数为 null 时引发 System::ArgumentNullException(_EXCEPTION_NULLPTR)
。
示例
此示例将 const char*
封送为 System::String
变量类型。
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
int main() {
const char* message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
要求
头文件:<msclr\marshal.h>、<msclr\marshal_windows.h>、<msclr\marshal_cppstd.h> 或 <msclr\marshal_atl.h>
命名空间: msclr::interop