Example #1
0
        /// <summary>Looks for CsvConverterAttribute on the property using PropertyInfo
        /// and then updates any relevant info on the map</summary>
        /// <param name="oneMap">The property map to examine</param>
        private void CreateAllUserSpecifiedConvertersForOneProperty(ColumnToPropertyMap oneMap)
        {
            List <CsvConverterAttribute> attributeList = oneMap.PropInformation.HelpFindAllAttributes <CsvConverterAttribute>();

            foreach (var oneAttribute in attributeList)
            {
                // In the case where the user wants to be explicit about ignoring a property, let them.
                // So, we will NOT have a converter for reading or writing according to this attribute.
                if (oneAttribute.IgnoreWhenReading && oneAttribute.IgnoreWhenWriting)
                {
                    oneMap.IgnoreWhenReading = true;
                    oneMap.IgnoreWhenWriting = true;
                    continue;
                }

                ICsvConverter converter = oneAttribute.CreateConverterForProperty(_theClassType, oneMap.PropInformation, _defaultFactory);

                bool isTypeConverter = CreateOneUserSpecifiedConverterForOneProperty(oneMap, converter, oneAttribute, true);

                // Only update column information for TYPE converters.
                // Pre and Post conveters should NOT contain column information!
                if (isTypeConverter)
                {
                    UpdateColumnInformation(oneMap, oneAttribute);
                }
            }
        }
Example #2
0
 private bool CreateOneUserSpecifiedConverterForOneProperty(ColumnToPropertyMap oneMap, ICsvConverter converter,
                                                            CsvConverterAttribute oneAttribute, bool throwExceptionIfConverterHasAlreadyBeenSpecified)
 {
     // Possible pre or post converter.
     if (oneAttribute is CsvConverterStringAttribute stringConverter &&
         (stringConverter.IsPreConverter || stringConverter.IsPostConverter))
     {
         AddOnePropertyPreOrPostConverter(oneMap, converter, stringConverter);
         return(false);
     }
Example #3
0
        private void CreateIgnoreColumnMap(List <ColumnToPropertyMap> columns, int columnIndex, string columnName)
        {
            var newItem = new ColumnToPropertyMap(null, columnIndex)
            {
                ColumnName        = columnName,
                ReadConverter     = null,
                IgnoreWhenReading = true,
            };

            columns.Add(newItem);
        }
Example #4
0
        private void CreateDefaultConverterForOnePropertyIfNecessary(ColumnToPropertyMap newMap)
        {
            // User doesn't want to use this column
            if (newMap.IgnoreWhenReading && newMap.IgnoreWhenWriting)
            {
                return;
            }

            // No converter specified for a type we understand
            if (newMap.ReadConverter == null && newMap.WriteConverter == null && IsTypeAllowed(newMap.PropInformation.PropertyType))
            {
                ICsvConverter converter = _defaultFactory.CreateConverter(newMap.PropInformation.PropertyType);
                newMap.ReadConverter  = newMap.IgnoreWhenReading ? null : converter;
                newMap.WriteConverter = newMap.IgnoreWhenWriting ? null : converter;
            }
        }
Example #5
0
        /// <summary>Maps all the properties on the T type class and then order then by column index.</summary>
        /// <returns>List of property maps sorted by column index</returns>
        public List <ColumnToPropertyMap> CreateWriteMap()
        {
            var mapList = new List <ColumnToPropertyMap>();

            foreach (PropertyInfo info in _theClassType.GetTypeInfo().GetProperties())
            {
                var oneMap = new ColumnToPropertyMap(info, _columnIndexDefaultValue);

                CreateAllUserSpecifiedConvertersForOneProperty(oneMap);

                mapList.Add(oneMap);
            }

            CreateAllUserSpecifiedConvertersOnTheClass(mapList);
            CreateDefaultConverterOnAnyPropertyThatDoesNotHaveAConverter(mapList);

            // Sort the columns the way the user wants them sorted or by column name
            return(mapList.Where(map => ShouldMapBeAdd(map))
                   .OrderBy(o => o.ColumnIndex)
                   .ThenBy(o => o.ColumnName)
                   .ToList());
        }