title: Is it legal to assign by ref to a field using IL?
body: <p>There is as far as I know no way to interact with reference types in Expression trees. (e.g. nothing emits a  <code>stind.*</code> or a <code>ldind.*</code> opcode). 
  I'm working on a bit of a rewriter to get around this annoyance. Since I'm building a new type that has the method body replaced with delegate invocations (to get around the fact that <code>CompileToMethod</code> can only do static methods that can't interact with new members). For by-ref and out parameters, I thought I'd replace their usages with <code>StrongBox&lt;T&gt;</code>. </p>
  
  <p>So if I came across a method that has a signature that looks like this::</p>
  
  <pre><code>public class SomeClass
  {
      public virtual bool SomeMethod(string arg1,ref int arg2)
      {
      }
  }
  </code></pre>
  
  <p>The override, the callbase method, and the delegate field I generate will look like this::</p>
  
  <pre><code>public class SomeClass&lt;1&gt; : SomeClass
  {
       private static bool SomeMethod&lt;0&gt;(
               SomeClass target,string arg1,StrongBox&lt;int&gt; arg2)
       {
          return call target.SomeMethod(arg1,ref arg2.Value)
       }
  
       private Func&lt;SomeClass,string,StrongBox&lt;int&gt;,bool&gt; &lt;0&gt;SomeMethod;
  
      public override bool SomeMethod(string arg1,ref int arg2)
      {
          StrongBox&lt;int&gt; box = new StrongBox&lt;int&gt;();
          box.Value = arg2;
          bool retVal = &lt;0&gt;SomeMethod.Invoke(this,arg1,box);
          arg2 = box.Value;
          return retVal;
      }
  }
  </code></pre>
  
  <p>However, this is quite a lot of code to perform this conversion, for each parameter it introduces a lot of complexity. It would be much easier when I perform the setting of <code>box.Value = arg2</code>, if I could do something like <code>&amp;box.Value = &amp;arg2</code> that is assign it's address to the address of arg2 as it stands. That way when the delegate performs a mutation on the value field the changes are forwarded. Doing this means I don't need to have to have a variable to hold the return value, and I don't need to perform a reference value update.</p>
  
  <p>Alternatively, if there is a way to perform assign-by-ref semantics with Expression trees, I'm all ears of course. </p>