Пример #1
0
 /// <summary>
 /// Specifies a custom fallback to be used if the value of a cell cannot be mapped.
 /// </summary>
 /// <typeparam name="TPropertyMap">The type of the property map.</typeparam>
 /// <param name="propertyMap">The property map to use.</param>
 /// <param name="fallbackItem">The fallback to be used if the value of a cell cannot be mapped.</param>
 /// <returns>The property map on which this method was invoked.</returns>
 public static TPropertyMap WithInvalidFallbackItem <TPropertyMap>(this TPropertyMap propertyMap, IFallbackItem fallbackItem) where TPropertyMap : ISinglePropertyMap
 {
     propertyMap.InvalidFallback = fallbackItem ?? throw new ArgumentNullException(nameof(fallbackItem));
     return(propertyMap);
 }
Пример #2
0
        private static bool AutoMap(this Type memberType, FallbackStrategy emptyValueStrategy, out ICellValueMapper mapper, out IFallbackItem emptyFallback, out IFallbackItem invalidFallback)
        {
            Type type = memberType.GetNullableTypeOrThis(out bool isNullable);

            Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray();

            IFallbackItem ReconcileFallback(FallbackStrategy strategyToPursue, bool empty)
            {
                // Empty nullable values should be set to null.
                if (empty && isNullable)
                {
                    return(new FixedValueFallback(null));
                }
                else if (strategyToPursue == FallbackStrategy.SetToDefaultValue || emptyValueStrategy == FallbackStrategy.SetToDefaultValue)
                {
                    return(new FixedValueFallback(type.DefaultValue()));
                }
                else
                {
                    Debug.Assert(emptyValueStrategy == FallbackStrategy.ThrowIfPrimitive);

                    // The user specified that we should set to the default value if it was empty.
                    return(new ThrowFallback());
                }
            }

            if (type == typeof(DateTime))
            {
                mapper          = new DateTimeMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(bool))
            {
                mapper          = new BoolMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type.GetTypeInfo().IsEnum)
            {
                mapper          = new EnumMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (type == typeof(string) || type == typeof(object) || type == typeof(IConvertible))
            {
                mapper          = new StringMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: false);
            }
            else if (type == typeof(Uri))
            {
                mapper          = new UriMapper();
                emptyFallback   = ReconcileFallback(FallbackStrategy.SetToDefaultValue, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else if (interfaces.Any(t => t == typeof(IConvertible)))
            {
                mapper          = new ChangeTypeMapper(type);
                emptyFallback   = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: true);
                invalidFallback = ReconcileFallback(FallbackStrategy.ThrowIfPrimitive, empty: false);
            }
            else
            {
                mapper          = null;
                emptyFallback   = null;
                invalidFallback = null;
                return(false);
            }

            return(true);
        }
 /// <summary>
 /// Specifies a custom fallback to be used if the value of a cell is empty.
 /// </summary>
 /// <typeparam name="TPropertyMap">The type of the property map.</typeparam>
 /// <param name="propertyMap">The property map to use.</param>
 /// <param name="fallbackItem">The fallback to be used if the value of a cell is empty.</param>
 /// <returns>The property map on which this method was invoked.</returns>
 public static TPropertyMap WithEmptyFallbackItem <TPropertyMap>(this TPropertyMap propertyMap, IFallbackItem fallbackItem) where TPropertyMap : IValuePipeline
 {
     propertyMap.EmptyFallback = fallbackItem ?? throw new ArgumentNullException(nameof(fallbackItem));
     return(propertyMap);
 }