title: Get accessors from PropertyInfo as Func<object> and Action<object> delegates
body: <p>I need to call properties that are determined at runtime through reflection and they are called at a high frequency. So I am looking for solution with optimal performance, which mean I'd probably avoid reflection. I was thinking of storing the property accessors as Func and Action delegates in a list and then call those. </p>
  
  <pre><code>private readonly Dictionary&lt;string, Tuple&lt;Func&lt;object&gt;, Action&lt;object&gt;&gt;&gt; dataProperties =
          new Dictionary&lt;string, Tuple&lt;Func&lt;object&gt;, Action&lt;object&gt;&gt;&gt;();
  
  private void BuildDataProperties()
  {
      foreach (var keyValuePair in this.GetType()
          .GetProperties(BindingFlags.Instance | BindingFlags.Public)
          .Where(p =&gt; p.Name.StartsWith("Data"))
          .Select(
              p =&gt;
                  new KeyValuePair&lt;string, Tuple&lt;Func&lt;object&gt;, Action&lt;object&gt;&gt;&gt;(
                      p.Name,
                      Tuple.Create(this.GetGetter(p), this.GetSetter(p)))))
      {
          this.dataProperties.Add(keyValuePair.Key, keyValuePair.Value);
      }
  }
  </code></pre>
  
  <p>The question now is, how do I get the accessor delagates as Func and Action delgates for later invokation?</p>
  
  <p>A naïve implementation that still uses reflection for the invocation would look like this:</p>
  
  <pre><code>private Func&lt;object&gt; GetGetter(PropertyInfo info)
  {
      // 'this' is the owner of the property
      return () =&gt; info.GetValue(this);
  }
  
  private Action&lt;object&gt; GetSetter(PropertyInfo info)
  {
      // 'this' is the owner of the property
      return v =&gt; info.SetValue(this, v);
  }
  </code></pre>
  
  <p>How could I implement the above methods without refelections. Would expressions be the fastest way? I have tried using expression like this:</p>
  
  <pre><code>private Func&lt;object&gt; GetGetter(PropertyInfo info)
  {
      // 'this' is the owner of the property
      return
          Expression.Lambda&lt;Func&lt;object&gt;&gt;(
              Expression.Convert(Expression.Call(Expression.Constant(this), info.GetGetMethod()), typeof(object)))
              .Compile();
  }
  
  private Action&lt;object&gt; GetSetter(PropertyInfo info)
  {
      // 'this' is the owner of the property
      var method = info.GetSetMethod();
      var parameterType = method.GetParameters().First().ParameterType;
      var parameter = Expression.Parameter(parameterType, "value");
      var methodCall = Expression.Call(Expression.Constant(this), method, parameter);
  
      // ArgumentException: ParameterExpression of type 'System.Boolean' cannot be used for delegate parameter of type 'System.Object'
      return Expression.Lambda&lt;Action&lt;object&gt;&gt;(methodCall, parameter).Compile();
  }
  </code></pre>
  
  <p>But here the last line of <code>GetSetter</code> I get the following excpetion if the type of the property is not exactly of type <code>System.Object</code>:</p>
  
  <blockquote>
    <p>ArgumentException: ParameterExpression of type 'System.Boolean' cannot
    be used for delegate parameter of type 'System.Object'</p>
  </blockquote>