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:
p.GetValue()
because is slow.getterInvocation.DynamicInvoke()
I solve the problem.
I need to pass the source object to the DynamicInvoke.
var value = getterInvocation.DynamicInvoke(source)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.