简体   繁体   中英

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:

在此处输入图片说明

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?

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 ) 

Access the columns directly using their column#, like record.GetField(3) or even record.GetField("Town/Area") etc.

Then you can compare,

if (String.Equals(record.GetField(3), "foo")) 
{
    //do my stuff.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM