private static UnaryExpression GetMethodBasedCoercionOperator(ExpressionType unaryType, Expression operand, Type convertToType, 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) && TypeUtils.AreEquivalent(method.ReturnType, convertToType))
{
return new UnaryExpression(unaryType, operand, method.ReturnType, method);
}
// check for lifted call
if ((operand.Type.IsNullableType() || convertToType.IsNullableType()) &&
ParameterIsAssignable(pms[0], operand.Type.GetNonNullableType()) &&
(TypeUtils.AreEquivalent(method.ReturnType, convertToType.GetNonNullableType()) ||
TypeUtils.AreEquivalent(method.ReturnType, convertToType)))
{
return new UnaryExpression(unaryType, operand, convertToType, method);
}
throw Error.OperandTypesDoNotMatchParameters(unaryType, method.Name);
}