Is it possible to avoid boxing when setting the fields of a C # structure / class using reflection?

Let's say I have a primitive value that I need to assign to some field using reflection. I know for sure that the field has the same primitive value type.

Is it possible to somehow 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*/);
}

PS This is not about latency indeed, it is about the possible pressure of GK. I use Unity3D, which uses the old version of Mono veeeery, which in turn uses a very sub-optimal GC implementation. Each additional memory allocation is calculated: (

PPS I am building my own interpreter in C #, avoiding reflection seems almost impossible.

+4
source share

Source: https://habr.com/ru/post/1618301/


All Articles