0

I'm trying to get dynamically the get value of a property, however, I got an exception:

System.Reflection.TargetParameterCountException: 'Parameter count mismatch.'

object result = null;

....

MethodInfo getMethod;
if ((getMethod = p.GetGetMethod(true)) != null)
{
    var openGetterType = typeof(Func<,>);
    var concreteGetterType = openGetterType.MakeGenericType(typeResult, p.PropertyType);
    var getterInvocation = Delegate.CreateDelegate(concreteGetterType, null, getMethod);

    var value = getterInvocation.DynamicInvoke(); //exception thrown here
    if (value != null)
    {
        p.SetValue(result, value, null);
    }
}

Remarks:

  • I'm trying to avoid using the p.GetValue() because is slow.
  • As I'm trying to invoke a getter I thought that it's not necessary to pass any value to getterInvocation.DynamicInvoke()
  • If speed is your concern, check https://stackoverflow.com/questions/26731159/fastest-way-for-get-value-of-a-property-reflection-in-c-sharp – Martheen Apr 29 '20 at 03:54

1 Answers1

0

I solve the problem.

I need to pass the source object to the DynamicInvoke.

var value = getterInvocation.DynamicInvoke(source)