title: How can I modify field inside Expression?
body: <p>I want to generate a method that will be similar to this:</p>
  
  <pre><code>void Add(ref MyStruct a, MyStruct b)
  {
      a.Field1 += b.Field1;
      // a.Field2 += b.Field2; // and so on
  }
  </code></pre>
  
  <p>To do so I declared my custom delegate:</p>
  
  <pre><code>public delegate void Add&lt;T&gt;(ref T a, T b) where T : struct;
  </code></pre>
  
  <p>and write my expression builder:</p>
  
  <pre><code>public static Expression&lt;Add&lt;T&gt;&gt; BuildExpression&lt;T&gt;() where T : struct
  {
      var t = typeof(T);
      var a = Expression.Parameter(t.MakeByRefType(), "a");
      var b = Expression.Parameter(t, "b");
  
      return Expression.Lambda&lt;Add&lt;T&gt;&gt;(
          Expression.Block(
              t.GetFields().Select(f =&gt; 
                  Expression.AddAssign(Expression.Field(a, f), Expression.Field(b, f)))),
          a, b
        );
  }
  </code></pre>
  
  <p>I've added <code>MyStruct</code> with a single field <code>Field1</code> to test it. When I inspect the <code>DebugView</code> of my expression it looks very similar to expected result:</p>
  
  <pre><code>.Lambda #Lambda1&lt;Program+Add`1[Program+MyStruct]&gt;(
      Program+MyStruct&amp; $a,
      Program+MyStruct $b) {
      .Block() {
          $a.Field1 += $b.Field1
      }
  }
  </code></pre>
  
  <p>however it does not modify first argument. Please see <a href="https://dotnetfiddle.net/N4Qwqz" rel="nofollow noreferrer">demo</a><br/></p>
  
  <p>So what I'm doing wrong in my <code>BuildExpression</code>?</p>