Exemplo n.º 1
0
        /// <summary>
        /// Map property to a column by specified column index(zero-based) and property name.
        /// </summary>
        /// <typeparam name="T">The target object type.</typeparam>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="columnIndex">The column index.</param>
        /// <param name="propertyName">The property name.</param>
        /// <param name="exportedColumnName">The column name for export.</param>
        /// <param name="tryTake">The function try to import from cell value to the target object.</param>
        /// <param name="tryPut">The function try to export source object to the cell.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper Map <T>(this Zian.NpoiCore.Mapper.Mapper mapper, ushort columnIndex, string propertyName, string exportedColumnName,
                                                          Func <IColumnInfo, object, bool> tryTake = null,
                                                          Func <IColumnInfo, object, bool> tryPut  = null)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            var type = typeof(T);
            var pi   = type.GetProperty(propertyName, MapHelper.BindingFlag);

            if (pi == null && type != typeof(object))
            {
                throw new InvalidOperationException($"Cannot find a public property in name of '{propertyName}'.");
            }

            var columnAttribute = new ColumnAttribute
            {
                Property     = pi,
                PropertyName = propertyName,
                Index        = columnIndex,
                Name         = exportedColumnName,
                TryPut       = tryPut,
                TryTake      = tryTake,
                Ignored      = false
            };

            return(mapper.Map(columnAttribute));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Map property to a column by specified column index(zero-based) and property selector.
        /// </summary>
        /// <typeparam name="T">The target object type.</typeparam>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="columnIndex">The column index.</param>
        /// <param name="propertySelector">Property selector.</param>
        /// <param name="exportedColumnName">The column name for export.</param>
        /// <param name="tryTake">The function try to import from cell value to the target object.</param>
        /// <param name="tryPut">The function try to export source object to the cell.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper Map <T>(this Zian.NpoiCore.Mapper.Mapper mapper, ushort columnIndex, Expression <Func <T, object> > propertySelector, string exportedColumnName,
                                                          Func <IColumnInfo, object, bool> tryTake = null,
                                                          Func <IColumnInfo, object, bool> tryPut  = null)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            var pi = MapHelper.GetPropertyInfoByExpression(propertySelector);

            if (pi == null)
            {
                throw new InvalidOperationException($"Cannot find the property specified by the selector.");
            }

            var columnAttribute = new ColumnAttribute
            {
                Property = pi,
                Index    = columnIndex,
                Name     = exportedColumnName,
                TryPut   = tryPut,
                TryTake  = tryTake,
                Ignored  = false
            };

            return(mapper.Map(columnAttribute));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Ignores all errors for the specified property.
        /// </summary>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="propertySelector">Property selector.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper IgnoreErrorsFor <T>(this Zian.NpoiCore.Mapper.Mapper mapper, Expression <Func <T, object> > propertySelector)
        {
            var pi = MapHelper.GetPropertyInfoByExpression(propertySelector);

            if (pi == null)
            {
                throw new InvalidOperationException($"Cannot find the property specified by the selector.");
            }

            var columnAttribute = new ColumnAttribute
            {
                Property     = pi,
                IgnoreErrors = true
            };

            return(mapper.Map(columnAttribute));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Map property to a column by specified column index(zero-based) and property selector.
        /// </summary>
        /// <typeparam name="T">The target object type.</typeparam>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="columnIndex">The column index.</param>
        /// <param name="propertySelector">Property selector.</param>
        /// <param name="tryTake">The function try to import from cell value to the target object.</param>
        /// <param name="tryPut">The function try to export source object to the cell.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper Map <T>(this Zian.NpoiCore.Mapper.Mapper mapper, ushort columnIndex, Expression <Func <T, object> > propertySelector,
                                                          Func <IColumnInfo, object, bool> tryTake = null,
                                                          Func <IColumnInfo, object, bool> tryPut  = null)
        {
            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            var pi = MapHelper.GetPropertyInfoByExpression(propertySelector);

            if (pi == null)
            {
                throw new InvalidOperationException($"Cannot find the property specified by the selector.");
            }

            return(mapper.Map(columnIndex, pi, tryTake, tryPut));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Map property to a column by specified column index(zero-based).
        /// </summary>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="columnIndex">The column index.</param>
        /// <param name="propertyInfo">The <see cref="PropertyInfo"/> object.</param>
        /// <param name="tryTake">The function try to import from cell value to the target object.</param>
        /// <param name="tryPut">The function try to export source object to the cell.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper Map(this Zian.NpoiCore.Mapper.Mapper mapper, ushort columnIndex, PropertyInfo propertyInfo,
                                                      Func <IColumnInfo, object, bool> tryTake = null,
                                                      Func <IColumnInfo, object, bool> tryPut  = null)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException(nameof(propertyInfo));
            }

            var columnAttribute = new ColumnAttribute
            {
                Property = propertyInfo,
                Index    = columnIndex,
                TryPut   = tryPut,
                TryTake  = tryTake,
                Ignored  = false
            };

            return(mapper.Map(columnAttribute));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Ignore property by names. Ignored properties will not be mapped for import and export.
        /// </summary>
        /// <typeparam name="T">The target object type.</typeparam>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="propertyNames">Property names.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper Ignore <T>(this Zian.NpoiCore.Mapper.Mapper mapper, params string[] propertyNames)
        {
            var type = typeof(T);

            foreach (var propertyName in propertyNames)
            {
                var pi = type.GetProperty(propertyName, MapHelper.BindingFlag);

                if (pi == null && type != typeof(object)) // Does not throw for dynamic type.
                {
                    throw new InvalidOperationException($"Cannot find a public property in name of '{propertyName}'.");
                }

                var columnAttribute = new ColumnAttribute
                {
                    Property     = pi,
                    PropertyName = propertyName,
                    Ignored      = true
                };
                mapper.Map(columnAttribute);
            }

            return(mapper);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Ignores all errors for the specified property.
        /// </summary>
        /// <param name="mapper">The <see cref="Mapper"/> object.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The mapper object.</returns>
        public static Zian.NpoiCore.Mapper.Mapper IgnoreErrorsFor <T>(this Zian.NpoiCore.Mapper.Mapper mapper, string propertyName)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            var type = typeof(T);
            var pi   = type.GetProperty(propertyName, MapHelper.BindingFlag);

            if (pi == null && type != typeof(object))
            {
                throw new InvalidOperationException($"Cannot find a public property in name of '{propertyName}'.");
            }

            var columnAttribute = new ColumnAttribute
            {
                Property     = pi,
                PropertyName = propertyName,
                IgnoreErrors = true
            };

            return(mapper.Map(columnAttribute));
        }