Exemplo n.º 1
0
        private void LoadFileButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = LoadFile("c:\\temp\\Person.csv");
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                LogMessage("Before overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }

                LogMessage("After overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        // Keep in mind that your replacement converter must be standalone.
                        // You cannot rely in any default converters.
                        // Default converters created when the service class is created.
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        // Replace a converter
                        reader.DefaultConverterFactory.ReplaceConverter(typeof(string), typeof(CsvConverterStringTextLengthEnforcer));

                        // Create all the column mappings.
                        reader.Init();

                        #region This converter has settings different that the defaults when it was constructed
                        // Well, you will have to find the columns with the converter that need changing

                        // We could just find string properties since it is the default, but that is problematic.
                        // What if a class property, which is a string, has a custom converter on it?
                        // So, let's just find the properties that are using our new default converter.
                        List <ColumnToPropertyMap> columnList = reader.FindColumnsByConverterType(typeof(CsvConverterStringTextLengthEnforcer));

                        foreach (ColumnToPropertyMap oneColumn in columnList)
                        {
                            if (oneColumn.ReadConverter is CsvConverterStringTextLengthEnforcer converter)
                            {
                                converter.MinimumLength = 5;
                                converter.MaximumLength = 6;
                                converter.CharacterToAddToShortStrings = '-';
                            }
                        }
                        #endregion

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }