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



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

相关内容

最新更新