I have the following code that I am using to try and parse a string to a generic type. In the instance I am using it fails when I try and parse to a TimeSpan. The input string is "12:34" which Parses fine using TimeSpan.Parse but I couldn’t find a solution to implement <Generic>.Parse
Public Function ParseGeneric(Of T)(ByVal stringValue As String) As T
Return DirectCast(Convert.ChangeType(stringValue, GetType(T)), T)
End Function
Error: Invalid cast from ‘System.String’ to ‘System.TimeSpan’.
If you’ll excuse some C#,
TypeDescriptor/TypeConvertercan help here:If I had to guess (completely untested) the VB for that: