title: Reflection Performance - Create Delegate (Properties C#)
body: <p>I'm having performance problems with using reflection.<br>
  So I decided to create delegates for the properties of my objects and so far got this:</p>
  
  <pre><code>TestClass cwp = new TestClass();
  var propertyInt = typeof(TestClass).GetProperties().Single(obj =&gt; obj.Name == "AnyValue");
  var access = BuildGetAccessor(propertyInt.GetGetMethod());
  var result = access(cwp);
  </code></pre>
  
  
  
  <pre><code>static Func&lt;object, object&gt; BuildGetAccessor(MethodInfo method)
  {
      var obj = Expression.Parameter(typeof(object), "o");
  
      Expression&lt;Func&lt;object, object&gt;&gt; expr =
          Expression.Lambda&lt;Func&lt;object, object&gt;&gt;(
              Expression.Convert(
                  Expression.Call(
                      Expression.Convert(obj, method.DeclaringType),
                      method),
                  typeof(object)),
              obj);
  
      return expr.Compile();
  }
  </code></pre>
  
  <p>The results were highly satisfactory, about 30-40 times faster than using the conventional method (<code>PropertyInfo.GetValue (obj, null);</code>)</p>
  
  <p>The problem is: <strong>How can I make a <code>SetValue</code> of a property, which works the same way? Unfortunately did not get a way.</strong></p>
  
  <p>I am doing so because I can not use methods with <code>&lt;T&gt;</code> because of the structure of my application.</p>