HOW TO:以原生型別存放實值型別的參考
更新:2007 年 11 月
請使用 Boxed 型別的 gcroot 以存放原生 (Native) 型別中實值型別 (Value Type) 的參考。
範例
// reference_to_value_in_native.cpp
// compile with: /clr
#using <mscorlib.dll>
#include <vcclr.h>
using namespace System;
public value struct V {
String ^str;
};
class Native {
public:
gcroot< V^ > v_handle;
};
int main() {
Native native;
V v;
native.v_handle = v;
native.v_handle->str = "Hello";
Console::WriteLine("String in V: {0}", native.v_handle->str);
}
String in V: Hello