我在将 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];