title: Improve performance on getting/setting values to properties on objects created from class created by reflection
body: <p>I'm having some problems to find a solution to this very specific issue. I have a class, let's call it &quot;MyClass&quot;, that serves as base to many other implementations we create via reflection on runtime. These implementations we create are classes that inherit &quot;MyClass&quot; and add a bunch of new string, double and DateTime properties to it (let's call them MyClassCustom). To access the values of those new properties on MyClassCustom, we have some methods on our base class that do some reflection shenanigans to get the data (more specifically the SetValue and GetValue from GetType().GetProperty(name)).</p>
  <p>Since we're trying to do bulk operations on those implementations, we have a concern on performance. I've navigated to some other issues like this one and found out that if I used delegates, the performance would be almost the same as if I used the direct accessors (200ms average for 10mi items versus previous 4~10s average) and it would look somewhat like this:</p>
  <pre><code>Type type = myClassCustomObject.GetType();
  Action&lt;MyClass, object&gt; setter = (Action&lt;MyClass, double&gt;)Delegate.CreateDelegate(typeof(Action&lt;MyClass, double&gt;), null, type.GetProperty(propertyName).GetSetMethod());
  Func&lt;MyClass, double&gt; getter = (Func&lt;MyClass, double&gt;)Delegate.CreateDelegate(typeof(Func&lt;MyClass, object&gt;), null, type.GetProperty(propertyName).GetGetMethod());
  
  var value = getter(myClassImplementation);
  setter(myClassImplementation, value + 1);
  </code></pre>
  <p>However this don't work (which is because I can't convert Action&lt;MyClassImplementation, double&gt; to Action&lt;MyClass, double&gt;) and when I try to wrap this up, I lose all the performance as if I was still doing the GetProperty(name).SetValue. See the code we tested below:</p>
  <pre><code>public class PropertyHelper {
    private static readonly ConcurrentDictionary&lt;Type, PropertyHelper[]&gt; Cache = new ConcurrentDictionary&lt;Type, PropertyHelper[]&gt;();
    private static readonly MethodInfo CallInnerGetDelegateMethod = typeof(PropertyHelper).GetMethod(nameof(CallInnerGetDelegate), BindingFlags.NonPublic | BindingFlags.Static);
    private static readonly MethodInfo CallInnerSetDelegateMethod = typeof(PropertyHelper).GetMethod(nameof(CallInnerSetDelegate), BindingFlags.NonPublic | BindingFlags.Static);
    
    public string Name { get; set; }
    public Func&lt;MyClass, object&gt; Getter { get; set; }
    public Action&lt;MyClass, object&gt; Setter { get; set; }
  
    public static PropertyHelper GetProperty(Type type, string propName) {
      return GetProperties(type).FirstOrDefault(x =&gt; x.Name == propName);
    }
  
    public static PropertyHelper[] GetProperties(Type type) {
      return Cache.GetOrAdd(type, _ =&gt; type.GetProperties().Select(property =&gt; {
        Type eventLogCustomType = property.DeclaringType;
        Type propertyType = property.PropertyType;
  
        Func&lt;MyClass, object&gt; getter = null;
        var getMethod = property.GetGetMethod();
        if (getMethod != null) {
            Type getMethodDelegateType = typeof(Func&lt;,&gt;).MakeGenericType(eventLogCustomType, propertyType);
            Delegate getMethodDelegate = Delegate.CreateDelegate(getMethodDelegateType, null, getMethod);
            
            MethodInfo callInnerGenericGetMethodWithTypes = CallInnerGetDelegateMethod.MakeGenericMethod(eventLogCustomType, propertyType);
            getter = (Func&lt;MyClass, object&gt;)callInnerGenericGetMethodWithTypes.Invoke(null, new[] { getMethodDelegate });
        }
  
        Action&lt;MyClass, object&gt; setter = null;
        var setMethod = property.GetSetMethod();
        if (setMethod != null) {
            Type setMethodDelegateType = typeof(Action&lt;,&gt;).MakeGenericType(eventLogCustomType, propertyType);
            Delegate setMethodDelegate = Delegate.CreateDelegate(setMethodDelegateType, null, setMethod);
  
            MethodInfo callInnerGenericSetMethodWithTypes = CallInnerSetDelegateMethod.MakeGenericMethod(eventLogCustomType, propertyType);
            setter = (Action&lt;MyClass, object&gt;)callInnerGenericSetMethodWithTypes.Invoke(null, new[] { setMethodDelegate });
        }
  
        return new PropertyHelper {
            Name = property.Name,
            Getter = getter,
            Setter = setter,
        };
      }).ToArray());
    }
    
    private static Func&lt;MyClass, object&gt; CallInnerGetDelegate&lt;TClass, TResult&gt;(Func&lt;TClass, TResult&gt; innerDelegate)
      where TClass : MyClass {
      return instance =&gt; innerDelegate((TClass)instance);
    }
  
    private static Action&lt;MyClass, object&gt; CallInnerSetDelegate&lt;TClass, TResult&gt;(Action&lt;TClass, TResult&gt; innerDelegate)
      where TClass : MyClass {
      return (instance, value) =&gt; innerDelegate((TClass)instance, (TResult)value);
    }
  }
  </code></pre>
  <p>I think I've exhausted my knowledge on this issue and don't know where else to look.</p>
  <p>Thanks in advance.</p>