mercredi 2 décembre 2015

Is it possible to avoid boxing when setting C# struct/class fields using reflection?

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*/);
}





Aucun commentaire:

Enregistrer un commentaire