Wrapper class needs to be
public ref class Wrapper
{
public:
static void WrappedFunction(float% r)
{ float copy = r; MyClass::Function(copy); r = copy; }
};
References defined with %
work almost exactly like ones defined using &
, except they work together with the garbage collector.
Another option is
public ref class Wrapper
{
public:
static void WrappedFunction(float% r)
{ pin_ptr<float> p = &r; MyClass::Function(*p); }
};
Which will pin the object in memory preventing the garbage collector from using it, but have the native code work directly on the managed variable. Not usually important, but if you had another thread needing to see intermediate values, this is how.