Replies: 1 comment
-
Take a look at what Depending on what you need to do, it may be useful to just call Maybe I should add a method to map by a string like |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I have a use-case for building ClassMap's at run-time from a collection of data that contains the property names for properties on classes used to represent data when reading or writing to csv.
I have been building class maps at run-time for classes without nested properties. But with complex properties (i.e. properties that are other reference types I define), not sure best way to build the ClassMap at run-time from a collection of data provided at run-time.
I put together a sample console app to demonstrate the use-case I'm trying to solve. Sample available at https://github.com/jlw109/CsvHelperDynamicClassMaps.git
Console app main method has three methods it calls.
The first method called is an example with mapping the preferred way when you have nested properties and have defined a class map at compile time.
Second method called is an example with mapping when you have nested properties and have defined a class map at compile time, but using legacy References method.
Last method called is the use-case I am trying to solve.
See method in program.cs called BuildDynamicCsvClassMap(). Comment explains goals of method that is incomplete.
This relates a little to #1633
Update 1
I have committed changes that provides one solution to my use-case. I suspect this can be improved for reliability and performance.
The example demonstrates how to use CsvHelper to read or write a csv file using a class map that is constructed at run time from a collection of custom objects defined by the class
The FieldName property must match a property name on the class being used for representing the records being read or written. The property name can represent nested properties using a dot between properties (e.g. Product.Name a nested property of Transaction in the example provided).
Beta Was this translation helpful? Give feedback.
All reactions