public void ReplacePropertyTest1() { var wrapper = new CsvRecordWrapper(); const string prop1Name = "Prop1"; const string prop2Name = "Prop2"; const string prop3Name = "Prop3"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); var prop2 = new CsvProperty(prop2Name, new string[] { "Hallo2" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); var prop3 = new CsvProperty(prop3Name, new string[] { "Hallo3" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); wrapper.AddProperty(prop1); wrapper.AddProperty(prop2); Assert.AreEqual(2, wrapper.Count); Assert.AreEqual(prop1Name, wrapper.PropertyNames[0]); Assert.AreEqual(prop2Name, wrapper.PropertyNames[1]); wrapper.ReplaceProperty(prop1Name, prop3); Assert.AreEqual(2, wrapper.Count); Assert.AreEqual(prop3Name, wrapper.PropertyNames[0]); Assert.AreEqual(prop2Name, wrapper.PropertyNames[1]); }
public void ReplacePropertyAtTest3() { var wrapper = new CsvRecordWrapper(); const string prop1Name = "Prop1"; const string prop2Name = "Prop2"; const string prop3Name = "Prop3"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); var prop2 = new CsvProperty(prop2Name, new string[] { "Hallo2" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); var prop3 = new CsvProperty(prop3Name, new string[] { "Hallo3" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); wrapper.AddProperty(prop1); wrapper.AddProperty(prop2); wrapper.ReplacePropertyAt(4711, prop3); }
public AccessBenchmark() { _csv = Properties.Resources.Test1; ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String); _indexWrapper = new CsvRecordWrapper(); _indexWrapper.AddProperty(new CsvIndexProperty("Column0", 0, conv)); _indexWrapper.AddProperty(new CsvIndexProperty("Column1", 1, conv)); _indexWrapper.AddProperty(new CsvIndexProperty("Column2", 2, conv)); _nameWrapper = new CsvRecordWrapper(); _nameWrapper.AddProperty(new CsvProperty("Column0", new string[] { "Column0" }, conv)); _nameWrapper.AddProperty(new CsvProperty("Column1", new string[] { "Column1" }, conv)); _nameWrapper.AddProperty(new CsvProperty("Column2", new string[] { "Column2" }, conv)); }
protected override void InitCsvRecordWrapperUndefinedValues(Tuple <string, ContactProp?, IList <string> > tpl, CsvRecordWrapper wrapper) { wrapper.AddProperty( new CsvProperty( tpl.Item1, tpl.Item3, StringConverter)); }
protected override void InitCsvRecordWrapperUndefinedValues(Tuple <string, ContactProp?, IList <string> > tpl, CsvRecordWrapper wrapper) { Debug.Assert(tpl.Item2.HasValue); if (tpl.Item2 == (ContactProp)AdditionalProp.Swap) { wrapper.AddProperty( new CsvProperty( tpl.Item1, tpl.Item3, CsvConverterFactory.CreateConverter(CsvTypeCode.String, false))); } else { wrapper.AddProperty( new CsvProperty( tpl.Item1, tpl.Item3, this.StringConverter)); } }
private static CsvRecordWrapper InitCsvRecordWrapper() { var wrapper = new CsvRecordWrapper(); // Store the stringConverter because you can reuse the same // converter for more than one property in CsvRecordWrapper. ICsvTypeConverter stringConverter = CsvConverterFactory.CreateConverter(CsvTypeCode.String, maybeDBNull: true); wrapper.AddProperty ( new CsvProperty(PUPILS_NAME, new string[] { PUPILS_NAME }, stringConverter) ); wrapper.AddProperty ( new CsvProperty(SUBJECT, new string[] { SUBJECT }, stringConverter) ); wrapper.AddProperty ( new CsvProperty(LESSON_DAY, new string[] { LESSON_DAY }, CsvConverterFactory .CreateEnumConverter <DayOfWeek>("G", maybeDBNull: true)) ); wrapper.AddProperty ( new CsvProperty(LESSON_BEGIN, new string[] { LESSON_BEGIN }, CsvConverterFactory .CreateConverter(CsvTypeCode.TimeSpan, maybeDBNull: true)) ); return(wrapper); }
public void ReplacePropertyTest4() { var wrapper = new CsvRecordWrapper(); const string prop1Name = "Prop1"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); wrapper.AddProperty(prop1); wrapper.ReplaceProperty(prop1Name, null !); }
public void TryGetMemberTest() { var wrapper = new CsvRecordWrapper(); const string prop1Name = "Prop1"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.Int32, true)); wrapper.AddProperty(prop1); dynamic dyn = wrapper; _ = dyn.Prop1; }
public void InsertPropertyTest5() { var wrapper = new CsvRecordWrapper(); const string prop1Name = "Prop1"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); var prop2 = new CsvProperty(prop1Name, new string[] { "Hallo2" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.String, true)); wrapper.AddProperty(prop2); wrapper.InsertProperty(0, prop1); }
public void DynPropTest(int wildcardTimeout) { var rec = new CsvRecord(new string[] { "Hallo1", "Blabla" }, false, false, true, false); var wrapper = new CsvRecordWrapper { Record = rec }; const string prop1Name = "Prop1"; const string prop2Name = "Prop2"; var prop1 = new CsvProperty(prop1Name, new string[] { "Hallo1" }, Converters.CsvConverterFactory.CreateConverter(Converters.CsvTypeCode.Int32, true)); wrapper.AddProperty(prop1); var prop2 = new CsvProperty(prop2Name, new string[] { "Blub", null !, "Bla*" },
public static void TestDeserializingClassesFromCsv() { const string csvFileName = "Objects.csv"; // Create a nonstandard CSV-File File.WriteAllText(csvFileName, new StringBuilder() .AppendLine( "Unterrichtstag;Unterrichtsbeginn;Vollständiger Name;Unterrichtsfach;") .AppendLine( "Wednesday;14:30;Susi Meyer;Piano") .AppendLine( "Thursday;15:15;Carl Czerny;Piano;") .AppendLine( ";;Frederic Chopin") .ToString()); // Initialize a CsvRecordWrapper which retrieves the data from // the CSV-Columns and converts it to the right data type. // Aliases with wildcards can be used to match the column-headers // of the CSV file. var wrapper = new CsvRecordWrapper(); // Reuse a converter for more than one property: ICsvTypeConverter stringConverter = CsvConverterFactory.CreateConverter(CsvTypeCode.String, nullable: true); wrapper.AddProperty ( new CsvProperty("Name", new string[] { "*name" }, stringConverter) ); wrapper.AddProperty ( new CsvProperty("Subject", new string[] { "*subject", "*fach" }, stringConverter) ); wrapper.AddProperty ( new CsvProperty("LessonDay", new string[] { "*day", "*tag" }, CsvConverterFactory .CreateEnumConverter <DayOfWeek>(nullable: true)) ); wrapper.AddProperty ( new CsvProperty("LessonBegin", new string[] { "*begin?" }, CsvConverterFactory .CreateConverter(CsvTypeCode.TimeSpan, nullable: true)) ); // Analyze the CSV file to determine the right parameters // for proper reading: var analyzer = new CsvAnalyzer(); analyzer.Analyze(csvFileName); // Read the CSV file: using var reader = new CsvReader(csvFileName, analyzer.HasHeaderRow, analyzer.Options, analyzer.FieldSeparator); var pupilsList = new List <Pupil>(); foreach (CsvRecord record in reader.Read()) { wrapper.Record = record; // Using a dynamic variable enables you to assign // the properties without having to explicitely cast them // to the target data type: dynamic dynWrapper = wrapper; pupilsList.Add(new Pupil { Name = dynWrapper.Name, LessonBegin = dynWrapper.LessonBegin, LessonDay = dynWrapper.LessonDay, Subject = dynWrapper.Subject }); } // Write the results to Console: foreach (Pupil pupil in pupilsList) { Console.WriteLine(pupil); Console.WriteLine(); } }