Search code examples
c#csvhelper

Access dynamic property with "/" in name


I am using CsvHelper to create a dynamic object from a CSV file.

The problem I have is that sometimes there are illegal characters in the name, like here:

enter image description here

Is there anyway that I can do a statement like:

if (record.Towm/Area == "foo") ...

Because it's dynamic I don't think I can use reflection to loop over it and check name with a string comparison.

Is there anyway I can access it through named array or some other method?


Solution

  • Since it's an ExpandoObject, you should be able to get properties with an indexer like you would an IDictionary:

    if(record["Town/Area"] == "foo") 
    

    Barring that, you can cast it to an IDictionary, and treat it as such.

    ((IDictionary<String, Object>)record)
    

    Then use linq for even more overengineered goodness:

    if( record.First(kvp => kvp.Key == "City/Town").Value == foo )