共用方式為


如何:以原生類型存放實值類型的參考

gcroot Boxed 型別上使用 來保存原生型別中實值型別的參考。

範例

// 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

另請參閱

使用 C++ Interop (隱含 PInvoke)