private static UnaryExpression GetMethodBasedUnaryOperator(ExpressionType unaryType, Expression operand, MethodInfo method)
{
Debug.Assert(method != null);
ValidateOperator(method);
ParameterInfo[] pms = method.GetParametersCached();
if (pms.Length != 1)
throw Error.IncorrectNumberOfMethodCallArguments(method, nameof(method));
if (ParameterIsAssignable(pms[0], operand.Type))
{
ValidateParamswithOperandsOrThrow(pms[0].ParameterType, operand.Type, unaryType, method.Name);
return new UnaryExpression(unaryType, operand, method.ReturnType, method);
}
// check for lifted call
if (operand.Type.IsNullableType() &&
ParameterIsAssignable(pms[0], operand.Type.GetNonNullableType()) &&
method.ReturnType.GetTypeInfo().IsValueType && !method.ReturnType.IsNullableType())
{
return new UnaryExpression(unaryType, operand, method.ReturnType.GetNullableType(), method);
}
throw Error.OperandTypesDoNotMatchParameters(unaryType, method.Name);
}