示例#1
0
        public List <T> GetGeoNames <T>()
        {
            List <ParserErrorInfo> ErrorInfos = new List <ParserErrorInfo>();
            List <Exception>       Exceptions = new List <Exception>();
            Func <ParserErrorInfo, Exception, bool> HandleEntryReadError = (info, ex) =>
            {
                ErrorInfos.Add(info);
                Exceptions.Add(ex);
                return(true);
            };

            Layout <GeoNameTxt> layout = new Layout <GeoNameTxt> .DelimitedLayout()
                                         .WithDelimiter("\t")
                                         .WithMember(o => o.countrycode)
                                         .WithMember(o => o.postalcode)
                                         .WithMember(o => o.placename)
                                         .WithMember(o => o.statename)
                                         .WithMember(o => o.statecode)
                                         .WithMember(o => o.countyorprovincename)
                                         .WithMember(o => o.countyorprovincecode)
                                         .WithMember(o => o.communityname)
                                         .WithMember(o => o.communitycode)
                                         .WithMember(o => o.latitude)
                                         .WithMember(o => o.longitude)
                                         .WithMember(o => o.accuracy);

            string            CountryTextFilePath = string.Format(TEXTFILENAME, Directory.GetCurrentDirectory(), TEXTFILESFOLDERNAME, typeof(T).Name.Substring(0, 2));
            List <GeoNameTxt> GeoNameTxts         = new List <GeoNameTxt>();

            using (var fileStream = File.OpenRead(CountryTextFilePath))
            {
                var flatfile = new FlatFile <GeoNameTxt>(layout, fileStream, Encoding.UTF8, HandleEntryReadError);
                foreach (var geoName in flatfile.Read())
                {
                    GeoNameTxts.Add(geoName);
                }
            }

            if (ErrorInfos.Count > 0 || Exceptions.Count > 0)
            {
                throw new Exception();
            }

            List <T> GeoNames = new List <T>();

            foreach (var geoName in GeoNameTxts)
            {
                object someCustomClassInstance = Activator.CreateInstance(typeof(T));
                foreach (PropertyInfo pi in typeof(T).GetProperties())
                {
                    var value = geoName.GetType().GetProperty(pi.Name).GetValue(geoName, null);
                    pi.SetValue(someCustomClassInstance, value);
                }
                GeoNames.Add((T)someCustomClassInstance);
            }

            ErrorInfos = null; Exceptions = null; layout = null; GeoNameTxts = null;

            return(GeoNames);
        }
示例#2
0
 private static IEnumerable<PersonFlat> ReadFile()
 {
     //using the flattmapper as the documentation
     using (var fileStream = File.OpenRead(DataFile))
     {
         var flatFile = new FlatFile<PersonFlat>(_layout, fileStream);
         return flatFile.Read()
             //we need a ToList here since flatmapper does iteractive reading, 
             //closing the stream before enumerating the collection will throw an error.
             .ToList();
     }
 }
示例#3
0
        public void can_write_read_stream()
        {
            using (var memory = new MemoryStream())
            {
                var flatFile = new FlatFile <TestObject>(layout, memory, HandleEntryReadError);
                flatFile.Write(objects);

                memory.Seek(0, SeekOrigin.Begin);

                var objectsAfterRead = flatFile.Read().ToList();
                Assert.True(objects.SequenceEqual(objectsAfterRead));
            }
        }
示例#4
0
        public void can_write_read_stream()
        {
            using (var memory = new MemoryStream())
            {
                var flatFile = new FlatFile<TestObject>(layout, memory, HandleEntryReadError);
                flatFile.Write(objects);

                memory.Seek(0, SeekOrigin.Begin);

                var objectsAfterRead = flatFile.Read().ToList();
                Assert.True(objects.SequenceEqual(objectsAfterRead));
            }
        }