This project has moved and is read-only. For the latest updates, please go here.

A little fine tuning of VersionConverter for .Net >= 4.0

Aug 16, 2013 at 7:26 PM
VersionConverter class makes use of the System.Version(string) constructor to deserialize a JSON string value.
This was the only solution proposed inside the .Net Framework 2.0 till 4.0.
The .Net 4.0 framework worsened the performance characteristics of this constructor and allowed an better way to parse a Version object from a string.
I propose to use them :
#if !(NET35 || NET20 || PORTABLE40)
           Version v = null;
          if (Version.TryParse((string)reader.Value, out v)) return v;
              throw JsonSerializationException.Create(reader, "Error parsing version string: {0}".FormatWith(CultureInfo.InvariantCulture, reader.Value));
#if (NET35 || NET20 || PORTABLE40)
            Version v = Version.Parse((string) reader.Value);
            return v;
          catch (Exception ex)
            throw JsonSerializationException.Create(reader, "Error parsing version string: {0}".FormatWith(CultureInfo.InvariantCulture, reader.Value), ex);
The first code block is lighter in its execution and creates a new exception.
The second is heavier as it re-throws an exception issued by the Version parser and as such is more precise about what didn't parse well in the string.
The code goes on between line 76 and line 77 in the VersionConverter.cs