【发布时间】:2020-04-29 03:28:38
【问题描述】:
我正在尝试动态获取属性的 get 值,但是遇到了异常:
System.Reflection.TargetParameterCountException: '参数计数不匹配。'
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);
}
}
备注:
- 我试图避免使用
p.GetValue(),因为它很慢。 - 当我尝试调用 getter 时,我认为没有必要将任何值传递给
getterInvocation.DynamicInvoke()
【问题讨论】:
-
如果您关心速度,请查看stackoverflow.com/questions/26731159/…