private static Expression AddOptionalConvert(TypeReference closureReturnType, TypeReference bodyReturnType, Expression constructedExpression) { if (!closureReturnType.Matches(bodyReturnType)) { var closureTypeEntity = closureReturnType.Entity as IType; var returnTypeEntity = bodyReturnType.Entity as IType; if (closureTypeEntity != null && returnTypeEntity != null && closureTypeEntity.IsValueType == false && returnTypeEntity.IsValueType == false) { return constructedExpression; } var convertCall = ReferenceExpression.Lift("System.Linq.Expressions.Expression.Convert"); constructedExpression = new MethodInvocationExpression(convertCall, constructedExpression, new TypeofExpression() { Type = closureReturnType }); } return constructedExpression; }