Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid cast from 'System.String' to 'System.TimeSpan'

Tags:

c#

I currently have a Generic method that reads an Value from the database based on a key and returns a specific type of that value.

    public T Get<T>(string key, T defaultValue)
    {
        var myparms = new List<SqlParameter>
                      {
                          new SqlParameter("@KEY", key),
                      };
        const string getPropertyQuery = "SELECT SPARM_VALUE FROM SYSPARAMS WHERE SPARM_KEY = @KEY;";

        var returnedValue = //Get value from Database

        if (returnedValue == null)
        {
            return defaultValue; //Value does not exists so return default.
        }

        return (T)Convert.ChangeType(returnedValue, typeof(T));
    }

But when I try and return a TimeSpan type I get the following exception. Invalid cast from 'System.String' to 'System.TimeSpan'.

After a bit of googling I found that the most common solution is to use the TimeSpan.Parse or TimeSpan.TryParse methods.

I also found TimeSpan Structure.

public struct TimeSpan : IComparable, IComparable, IEquatable, IFormattable

My question is why, why is TimeSpan not able to do this. Is it because it doesn't have the IConvertible interface? Any thoughts on this would be much appreciated.

like image 705
Jethro Avatar asked Aug 12 '11 10:08

Jethro


2 Answers

I don't think Convert.ChangeType is what you really want. Try TypeConverter.ConvertFrom:

var converter = TypeDescriptor.GetConverter(typeof(T));
return converter.CanConvertFrom(returnedValue) ? 
    (T)converter.ConvertFrom(returnedValue) :
    defaultValue;
like image 190
Anton Gogolev Avatar answered Oct 24 '22 06:10

Anton Gogolev


There isnt an implicit conversion between String and TimeSpan, you need to convert explicitly using Parse or TryParse

like image 1
Dean Chalk Avatar answered Oct 24 '22 08:10

Dean Chalk