lundi 26 février 2018

Convert sql server datetimeoffset to C# datetimeoffset or datetime

I have a column with datetimeoffset data type. For e.g.

2018-02-19 10:08:53.8060711 +00:00

I am trying to set this value to the C# property having the same data type.

In my application, I am using reflection to set the properties into the model (can't avoid reflection)

Here is the sample code -

Type propType = typeof(DateTimeOffset);
string value = "2018-02-19 10:08:53.8060711 +00:00";
// Failing here
propInfo.SetValue(model, Convert.ChangeType(value,propType));

Please note that the code is working for other datatypes. For e.g.

Type propType = typeof(bool);
string value = "true";
// This is working
propInfo.SetValue(model, Convert.ChangeType(value,propType));

If we can assign this value to DateTime instead of DateTimeOffset, that would also work for me





Aucun commentaire:

Enregistrer un commentaire