title: Field getter/setter with expression tree in base class
body: <p>Following the examples on <a href="https://stackoverflow.com/questions/321650/how-do-i-set-a-field-value-in-an-c-sharp-expression-tree">this post</a> and <a href="https://stackoverflow.com/questions/320500/how-do-i-generate-a-compiled-lambda-with-method-calls">its follow-up question</a>, I am trying to create field getters / setters using compiled expressions.</p>
  
  <p>The getter works just great, but I am stuck the setter, as I need the setter to assign any type of fields.</p>
  
  <p>Here my setter-action builder:</p>
  
  <pre><code>public static Action&lt;T1, T2&gt; GetFieldSetter&lt;T1, T2&gt;(this FieldInfo fieldInfo) {
    if (typeof(T1) != fieldInfo.DeclaringType &amp;&amp; !typeof(T1).IsSubclassOf(fieldInfo.DeclaringType)) {
      throw new ArgumentException();
    }
    ParameterExpression targetExp = Expression.Parameter(typeof(T1), "target");
    ParameterExpression valueExp = Expression.Parameter(typeof(T2), "value");
    //
    // Expression.Property can be used here as well
    MemberExpression fieldExp = Expression.Field(targetExp, fieldInfo);
    BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
    //
    return Expression.Lambda&lt;Action&lt;T1, T2&gt;&gt; (assignExp, targetExp, valueExp).Compile();
  }
  </code></pre>
  
  <p>Now, I store the generic setters into a cache list (because of course, building the setter each time is a performance killer), where I cast them as simple "objects":</p>
  
  <pre><code> // initialization of the setters dictionary
   Dictionary&lt;string, object&gt; setters = new Dictionary(string, object)();
   Dictionary&lt;string, FieldInfo&gt; fldInfos = new Dictionary(string, FieldInfo)();
   FieldInfo f = this.GetType().GetField("my_int_field");
   setters.Add(f.Name, GetFieldSetter&lt;object, int&gt;(f); 
   fldInfos.Add(f.Name, f); 
   //
   f = this.GetType().GetField("my_string_field");
   setters.Add(f.Name, GetFieldSetter&lt;object, string&gt;(f); 
   fldInfos.Add(f.Name, f); 
  </code></pre>
  
  <p>Now I try to set a field value like this:</p>
  
  <pre><code> void setFieldValue(string fieldName, object value) {
        var setterAction = setters[fieldName];
        // TODO: now the problem =&gt; how do I invoke "setterAction" with 
        // object and fldInfos[fieldName] as parameters...?
   }
  </code></pre>
  
  <p>I could simply call a generic method and cast each time, but I am worried about the performance overhead... Any suggestions?</p>
  
  <p>-- <strong>EDITED ANSWER</strong>
  Based on <a href="https://stackoverflow.com/a/38529501/2490877">Mr Anderson's answer</a>, I created a small test program which compares directly setting the value, cached reflection (where FieldInfo's are cached) and the cached multi-type code. I use object inheritance with up to 3 level of inheritance (<code>ObjectC : ObjectB : ObjectA</code>). </p>
  
  <p><a href="https://eswys.com/StackOverflow/perfTester.zip" rel="nofollow noreferrer">Full code is of the example can be found here.</a></p>
  
  <p>Single iteration of the test gives following output:</p>
  
  <pre><code>-------------------------
  ---      OBJECT A     ---
  -------------------------
    Set direct:       0.0036 ms
    Set reflection:   2.319 ms
    Set ref.Emit:     1.8186 ms
    Set Accessor:     4.3622 ms
  
  -------------------------
  ---      OBJECT B     ---
  -------------------------
    Set direct:       0.0004 ms
    Set reflection:   0.1179 ms
    Set ref.Emit:     1.2197 ms
    Set Accessor:     2.8819 ms
  
  -------------------------
  ---      OBJECT C     ---
  -------------------------
    Set direct:       0.0024 ms
    Set reflection:   0.1106 ms
    Set ref.Emit:     1.1577 ms
    Set Accessor:     2.9451 ms
  </code></pre>
  
  <p>Of course, this simply shows the cost of creating the objects - this allows us to measure the offset of creating the cached versions of Reflection and Expressions.</p>
  
  <p>Next, let's run 1.000.000 times:</p>
  
  <pre><code>-------------------------
  ---      OBJECT A     ---
  -------------------------
    Set direct:       33.2744 ms
    Set reflection:   1259.9551 ms
    Set ref.Emit:     531.0168 ms
    Set Accessor:     505.5682 ms
  
  -------------------------
  ---      OBJECT B     ---
  -------------------------
    Set direct:       38.7921 ms
    Set reflection:   2584.2972 ms
    Set ref.Emit:     971.773 ms
    Set Accessor:     901.7656 ms
  
  -------------------------
  ---      OBJECT C     ---
  -------------------------
    Set direct:       40.3942 ms
    Set reflection:   3796.3436 ms
    Set ref.Emit:     1510.1819 ms
    Set Accessor:     1469.4459 ms
  </code></pre>
  
  <p>For the sake of completeness: I removed the call to the "set" method to highlight the cost of getting the setter (<code>FieldInfo</code> for the reflection method, <code>Action&lt;object, object&gt;</code> for the expression case). Here the results:</p>
  
  <pre><code>-------------------------
  ---      OBJECT A     ---
  -------------------------
    Set direct:       3.6849 ms
    Set reflection:   44.5447 ms
    Set ref.Emit:     47.1925 ms
    Set Accessor:     49.2954 ms
  
  
  -------------------------
  ---      OBJECT B     ---
  -------------------------
    Set direct:       4.1016 ms
    Set reflection:   76.6444 ms
    Set ref.Emit:     79.4697 ms
    Set Accessor:     83.3695 ms
  
  -------------------------
  ---      OBJECT C     ---
  -------------------------
    Set direct:       4.2907 ms
    Set reflection:   128.5679 ms
    Set ref.Emit:     126.6639 ms
    Set Accessor:     132.5919 ms
  </code></pre>
  
  <p>NOTE: time increase here is not due to the fact that access times are slower for larger dictionaries (as they have <code>O(1)</code> access times), but due to the fact that the number of times we access it is increased (4 times per iteration for <code>ObjectA</code>, 8 for <code>ObjectB</code>, 12 for <code>ObjectC</code>)... As one sees, only the creation offset makes a difference here (which is to be expected).</p>
  
  <p>Bottom line: we did improve performance by a factor of 2 or more, but we're still far away from the direct field set's performance... Retrieving the correct setter in the list represents a good 10% of the time.</p>
  
  <p>I'll try with expression trees in place of Reflection.Emit to see if we can further reduce the gap... Any comment is more than welcome.</p>
  
  <p><strong><em>EDIT 2</em></strong>
  I added results using the approach using a generic "Accessor" class as suggested by <a href="https://stackoverflow.com/users/276083/eli-arbel">Eli Arbel</a> on <a href="https://stackoverflow.com/a/38538377/2490877">this post</a>.</p>