static LambdaExpression OptionalConversion(Type type, ExcelParameterRegistration paramReg, bool treatEmptyAsMissing, bool treatNAErrorAsMissing)
{
// Decide whether to return a conversion function for this parameter
if (!paramReg.CustomAttributes.OfType <OptionalAttribute>().Any())
{
return(null);
}
var defaultAttribute = paramReg.CustomAttributes.OfType <DefaultParameterValueAttribute>().FirstOrDefault();
var defaultValue = defaultAttribute == null?TypeConversion.GetDefault(type) : defaultAttribute.Value;
// var returnType = type.GetGenericArguments()[0]; // E.g. returnType is double
// Consume the attributes
paramReg.CustomAttributes.RemoveAll(att => att is OptionalAttribute);
paramReg.CustomAttributes.RemoveAll(att => att is DefaultParameterValueAttribute);
// Here's the actual conversion function
var input = Expression.Parameter(typeof(object), "input");
return
(Expression.Lambda(
Expression.Condition(
MissingTest(input, treatEmptyAsMissing, treatNAErrorAsMissing),
Expression.Constant(defaultValue, type),
TypeConversion.GetConversion(input, type)),
input));
}