Ejemplo n.º 1
0
        /// <summary>
        /// Map by a <see cref="ColumnAttribute"/> object.
        /// </summary>
        /// <param name="attribute">The <see cref="ColumnAttribute"/> object.</param>
        /// <returns>The mapper object.</returns>
        public Mapper Map(ColumnAttribute attribute)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            if (attribute.Property != null)
            {
                attribute.MergeTo(Attributes);
            }
            else if (attribute.PropertyName != null)
            {
                if (DynamicAttributes.ContainsKey(attribute.PropertyName))
                {
                    DynamicAttributes[attribute.PropertyName].MergeFrom(attribute);
                }
                else
                {
                    // Ensures column name for the first time mapping.
                    if (attribute.Name == null)
                    {
                        attribute.Name = attribute.PropertyName;
                    }

                    DynamicAttributes[attribute.PropertyName] = attribute;
                }
            }
            else
            {
                throw new InvalidOperationException("Either PropertyName or Property should be specified for a valid mapping!");
            }

            return(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads attributes to a dictionary.
        /// </summary>
        /// <param name="attributes">Container to hold loaded attributes.</param>
        /// <param name="type">The type object.</param>
        public static void LoadAttributes(Dictionary <PropertyInfo, ColumnAttribute> attributes, Type type)
        {
            if (type == null)
            {
                return;
            }

            foreach (var pi in type.GetProperties(BindingFlag))
            {
                var columnMeta      = pi.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() as ColumnAttribute;
                var ignore          = Attribute.IsDefined(pi, typeof(IgnoreAttribute));
                var useLastNonBlank = Attribute.IsDefined(pi, typeof(UseLastNonBlankValueAttribute));

                if (columnMeta == null && !ignore && !useLastNonBlank)
                {
                    continue;
                }

                if (columnMeta == null)
                {
                    columnMeta = new ColumnAttribute
                    {
                        Ignored = ignore ? new bool?(true) : null,
                        UseLastNonBlankValue = useLastNonBlank ? new bool?(true) : null
                    }
                }
                ;

                columnMeta.Property = pi;

                // Note that attribute from Map method takes precedence over Attribute meta data.
                columnMeta.MergeTo(attributes, false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Map by a <see cref="ColumnAttribute"/> object.
        /// </summary>
        /// <param name="attribute">The <see cref="ColumnAttribute"/> object.</param>
        /// <returns>The mapper object.</returns>
        public Mapper Map(ColumnAttribute attribute)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            if (attribute.Property != null)
            {
                attribute.MergeTo(Attributes);
            }
            else if (attribute.PropertyName != null)
            {
                DynamicAttributes[attribute.PropertyName] = attribute;
            }
            else
            {
                throw new InvalidOperationException("Either PropertyName or Property should be specified for a valid mapping!");
            }

            return(this);
        }