title: Direct memory access to underlying field data
body: <p>I'm looking for a way to avoid FieldInfo.Get/SetValue overhead, and access memory directly for a few select, known ahead of time, primitive types. (Most specifically, I'm looking to avoid any memory allocations in our custom serializer)</p>
  
  <p>Basically, here's what the official way allows me to do:</p>
  
  <pre><code>System.Object o = someobject;
  int inOut = 0;
  
  var type = o.GetType();
  var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
  foreach (var fi in fieldInfos) {
      fi.SetValue(o, inOut);
      inOut = (int)fi.GetValue(o);
  }
  </code></pre>
  
  <p>And here's roughly what I'd like to do:</p>
  
  <pre><code>foreach (var fi in fieldInfos) {
      fixed(int* ip = o.basePointer + fi.fieldOffset) {
          *p = inOut;
          inOut = *p;
      }
  }
  </code></pre>
  
  <p>I would use this only for Int32, Single, and possibly bools. I'm primarily interested in getting this working on Mono, so if there's anything Mono specific available, that'd be fine.</p>
  
  <p>Note: I'm well aware of the "you shouldn't be doing this", and "have you profiled it" etc. I know, and I have, which is why I'm looking into this. We have a very specific case, where we control all variables (and all code), but we would like it to work on any 'normal' class without requiring additional markup or explicit struct layout.</p>
  
  <p>EDIT: I should point out that I'm not able to emit dynamic code to solve this. I'm ok with a solution requiring me to write and assemble IL up-front though.</p>