1

I have created a modelbinder using reflection to set property values from a csv file. All values parsed from the file are strings. I am using Convert.ChangeType which has worked for most cases to convert to relevant types i.e. int, long etc. but now a value "20180227" for a date needs to be parsed to my DateTime property. The format in the csv is yyyyMMdd. The code I have so far:

foreach (var property in matchedModel)
{
    Type propType = Nullable.GetUnderlyingType(property.Value.PropertyType) ?? 
                                               property.Value.PropertyType;

    var value = string.IsNullOrEmpty(data.Value[property.Key]) ? null : 
                Convert.ChangeType(data.Value[property.Key], propType);

    property.Value.SetValue(instance, value, null);
}

data.Value[property.Key] is what holds my parsed string.

The property variable is the relevant property that needs to be set. Since I had some nullable properties I had to use the Nullable.GetUnderlyingType.

The problem is that Convert.ChangeType can't infer how to properly change my date format into a DateTime object. What I could do is something like:

if(property is datetime) do value conversion

However this would be too hardcoded and I want something more generic/dynamic. What would be the best way to approach this?

Usman Khan
  • 676
  • 1
  • 7
  • 20
  • What is the value of `propType` when it's looking at this datetime value? – Reinstate Monica Cellio Dec 21 '18 at 11:11
  • @Archer The value of `propType` is Name = "DateTime" FullName = "System.DateTime" – Usman Khan Dec 21 '18 at 11:14
  • What type does it convert it to? You've said it's explained above, but it's not. – Reinstate Monica Cellio Dec 21 '18 at 11:44
  • @Archer The `data.Value[property.Key]` is of type string which in this instance will hold 20180227 which is yyyyMMdd format. I need to convert this string to a proper DateTime but it isn't accepting the format. – Usman Khan Dec 21 '18 at 11:48
  • @Archer It was always a string, I am trying to convert it into DateTime. My parser takes all values as strings. – Usman Khan Dec 21 '18 at 11:50
  • Yes, I know it is a string. What you describe cannot happen. This is what you have described... *I have tried to cast a string as a datetime and populate a datetime property with it but it is still a string*. Please try to clearly explain what is actually happening. – Reinstate Monica Cellio Dec 21 '18 at 11:51
  • @Archer I have edited the question to hopefully clear things up. – Usman Khan Dec 21 '18 at 12:02
  • 1
    Possible duplicate of [How to convert DateTime to/from specific string format (both ways, e.g. given Format is "yyyyMMdd")?](https://stackoverflow.com/questions/3477735/how-to-convert-datetime-to-from-specific-string-format-both-ways-e-g-given-fo) – CSharpie Dec 21 '18 at 12:04
  • 2
    Since it's not a valid datetime format, I think you do have to check the type and cast it yourself. I don't think there's a generic way of saying *cast this var to this type but just make it work if it's invalid*, which is pretty much what you're asking for. It's still generic, but handling invalid data. The link above, from @CSharpie, will give you what you need to do that. – Reinstate Monica Cellio Dec 21 '18 at 12:57
  • Did a bit of research, turns out `ICustomFormatter` is what I needed. Thanks for the help anyway – Usman Khan Dec 21 '18 at 13:59

0 Answers0