كيفية القيام بما يلي: الاحتفاظ بـمرجع لنوع قيمة في النوع الأصلي

استخدم gcroot على النوع المحول إلى كائن (boxed 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 

راجع أيضًا:

المرجع

استخدام PInvoke) C++ Interop الضمني )