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()