如何使用反射将 ref 参数从托管 C++ 传递到 C# 方法

How to pass a ref parameter from managed c++ to c# method using reflection

我在将 ref 参数从托管 C++ 包装程序传递到动态加载库的 C# 方法时遇到问题。 参数返回值为0.

C# 方法

void method(ref int param)

具有跟踪参考的 C++/CLI 包装器调用方法

Assembly^ assembly = Assembly::LoadFrom(assemblyName);
Type^ type = assembly->GetType(typeName);
gcroot<Object^> instance = Activator::CreateInstance(type);
MethodInfo^ method = instance->GetType()->GetMethod(methodName);

System::Int32^% refParam = gcnew System::Int32;
method->Invoke(instance, gcnew array<Object^> { refParam });
//refParam value is 0

我能够从传递给 Invoke 方法的数组中读取更新值。

array<Object^>^ args = gcnew array<Object^> { refParam };
method->Invoke(instance, args);

int value = (int)args[0];