private static Expression GetCallExpression( MethodInfo methodInfo, ParameterExpression instanceParameter, Type instanceType, Type returnType, IEnumerable <ParameterInfo> parameterInfos, IEnumerable <Expression> parameterExpressions) { if (methodInfo == null) { throw new ArgumentNullException("methodInfo"); } returnType = returnType ?? typeof(void); parameterInfos = parameterInfos ?? Enumerable.Empty <ParameterInfo>(); parameterExpressions = parameterExpressions ?? Enumerable.Empty <ParameterExpression>(); var coercedParameters = parameterExpressions.Zip( parameterInfos, (parameterExpression, parameterInfo) => parameterExpression.Coerce(parameterExpression.Type, parameterInfo.ParameterType)); MethodCallExpression call; if (methodInfo.IsStatic) { call = Expression.Call(methodInfo, coercedParameters); } else { if (instanceParameter == null) { throw new ArgumentNullException("instanceParameter", "'instanceParameter' cannot be null when 'methodInfo' is not static."); } if (instanceType == null) { throw new ArgumentNullException("instanceType", "'instanceType' cannot be null when 'methodInfo' is not static."); } var instanceCast = instanceParameter.Coerce(instanceType, methodInfo.DeclaringType); call = Expression.Call(instanceCast, methodInfo, coercedParameters); } var callCast = call.Coerce(methodInfo.ReturnType, returnType); return(callCast); }
private static MemberExpression GetField(FieldInfo fieldInfo, Type declaringType, ParameterExpression instanceParameter) { MemberExpression field; if (fieldInfo.IsStatic) { field = Expression.Field(null, fieldInfo); } else { var instanceCast = instanceParameter.Coerce(declaringType, fieldInfo.DeclaringType); field = Expression.Field(instanceCast, fieldInfo); } return(field); }
private static Expression GetNonGenericCallExpression( MethodInfo methodInfo, ParameterExpression instanceParameter, IEnumerable <ParameterInfo> parameterInfos, ParameterExpression parameter) { if (methodInfo == null) { throw new ArgumentNullException("methodInfo"); } parameterInfos = parameterInfos ?? Enumerable.Empty <ParameterInfo>(); var coercedParameters = parameterInfos.Select( (parameterInfo, i) => Expression.ArrayAccess(parameter, Expression.Constant(i)).Coerce(typeof(object), parameterInfo.ParameterType)); MethodCallExpression call; if (methodInfo.IsStatic) { call = Expression.Call(methodInfo, coercedParameters); } else { if (instanceParameter == null) { throw new ArgumentNullException("instanceParameter", "'instanceParameter' cannot be null when 'methodInfo' is not static."); } var instanceCast = instanceParameter.Coerce(typeof(object), methodInfo.DeclaringType); call = Expression.Call(instanceCast, methodInfo, coercedParameters); } var callCast = call.Coerce(methodInfo.ReturnType, typeof(object)); return(callCast); }