示例#1
0
        public void WithCellValueMappers_ValidMappers_ThrowsArgumentNullException()
        {
            SingleExcelPropertyMap <string> propertyMap = Map(t => t.Value);
            ICellValueMapper mapper1 = Assert.Single(propertyMap.CellValueMappers);
            ICellValueMapper mapper2 = new BoolMapper();;

            Assert.Same(propertyMap, propertyMap.WithCellValueMappers(mapper2));
            Assert.Equal(new ICellValueMapper[] { mapper1, mapper2 }, propertyMap.CellValueMappers);
        }
示例#2
0
        /// <summary>
        /// Adds the given mapper to the pipeline of cell value mappers.
        /// </summary>
        /// <param name="mapper">The mapper to add.</param>
        public void AddCellValueMapper(ICellValueMapper mapper)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            _cellValueMappers.Add(mapper);
        }
示例#3
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);
        }