Say I have a primitive value which I need to assign to some field using reflection. I know for sure that field is of the same primitive value type.
Is it possible somehow to set this value without boxing?
void SetFloat(object o, string name, float val)
{
var type = o.GetType();
var fld = type.GetField(name);
fld.SetValue(o, val /*boxing happens here*/);
}
P.S. It's not about latency really, it's about possible GC pressure. I'm using Unity3D which uses veeeery old Mono version which in its turn uses a very non-optimal GC implementation. Every extra memory allocation counts :(
P.P.S I'm building my own C# based interpreter, avoiding reflection seems almost impossible.
FieldInfo.SetValue()
only has overloads acceptingobject newValue
, so any value type you pass it will be boxed. – BowstringReflection.Emit
orExpression.Compile
? – Simard__makeref
andSetValueDirect
? – Digestible